Linq动态查询
public class ExpressionCall
{
List<Customer> customers = new List<Customer>() {
new Customer() { CustomerID = "A001"},
new Customer() { CustomerID = "A"},
new Customer() { CustomerID = "B001" },
};
string[] starts = { "A", "C", "D" };
public void SelectMore()
{ //根据CustomerID首字母 包含A,C,D动态创建查询
IQueryable<Customer> cus = customers.AsQueryable();
ParameterExpression c = Expression.Parameter(typeof(Customer), "c");
Expression condition = Expression.Constant(false);
foreach (string s in starts)
{
Expression con = Expression.Call(
Expression.Property(c, typeof(Customer).GetProperty("CustomerID")),
typeof(string).GetMethod("Equals", new Type[] { typeof(string) }),
Expression.Constant(s));
condition = Expression.Or(con, condition);
}
Expression<Func<Customer, bool>> end =
Expression.Lambda<Func<Customer, bool>>(condition, new ParameterExpression[] { c });
var cu = cus.Where(end);
}
}
public class Customer
{
public string CustomerID { get; set; }
}
代码如上,参考:http://www.cnblogs.com/blusehuang/archive/2007/07/13/816970.html
其中包含。首字母包含
typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }),
typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
Linq动态查询的更多相关文章
- Linq动态查询与模糊查询 ---转
Linq动态查询与模糊查询(带源码示例) 继LINQ动态组合查询PredicateExtensions讲解 ----- 在用上面的方法时遇到了些问题 解决 LINQ to Entities 不支持 L ...
- Linq 动态查询排序
Linq的排序一般是这样写的: query.OrderBy(x => x.Tel).Skip().Take(); 实际使用中排序字段可能是通过字符类型的参数来设置的,于是想这样实现: query ...
- Linq动态查询简易解决之道(原创)
因为项目需要使用Linq来查询数据,但是在多条件查询时,需要使用一大堆if(...!=string.empty)等判断条件感觉不是很优雅.网上搜索以下,大概找到了两种办法,一种是老外写的一个类,感觉用 ...
- 表达式树在LINQ动态查询
动态构建表达式树,最佳实践版,很实用! public class FilterCollection : Collection<IList<Filter>> { public F ...
- c#——表达式树在LINQ动态查询
一般如果逻辑比较简单,只是存在有的情况多一个查询条件,有的情况不需要添加该查询条件 简单方式这样操作就可以了 public IQueryable<FileImport> DynamicCh ...
- linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ
在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...
- LINQ动态查询类--[DynamicLinqExpressions]
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.L ...
- 用PredicateBuilder实现Linq动态拼接查询
在使用Linq查询的时候,特别是如果你在使用Entiry Framwork,有时会遇到动态查询的情况(客户的查询条件是不固定的拼接查询).我们能想到的第一方案应该是拼接SQL,的确这样是可以达到我们的 ...
- Linq to sql 实现多条件的动态查询(方法一)
/// <summary> /// Linq to sql 多字段动态查询 /// </summary> /// <returns></returns> ...
随机推荐
- NO.2 安装配置
检测当前系统下的jdk安装情况: [root@Centos 桌面]# rpm -qa | grep java tzdata-java-2012j-1.el6.noarch java-1.7.0-ope ...
- F3D模式规则详解
F3D有两个版本,长期版还有短期版 长期版规则 1.购买时候分配 第一队 20% to 奖金池, 56%分给所有人, 30% 持有p3d的人第二队 35% to 奖金池, 43%分给所有人, 8% 持 ...
- MongoDB GridFS 存储文件
使用MongoDB的GridFS方式. CSDN: https://blog.csdn.net/qq_32657967/article/details/81534259官方文档: https://do ...
- c#判断是否有网络
//调用操作系统API [System.Runtime.InteropServices.DllImport("wininet")] private extern static bo ...
- PHP读取HTML生成doc
PHP代码如下: <?php //获取1.html文档的内容(包括html代码) $result = file_get_contents('./word.html'); echo "$ ...
- k8s(4)-使用服务公开应用程序
Kubernetes中的服务是一个抽象,它定义了一组逻辑Pod和一个访问它们的策略.服务允许从属Pod之间的松散耦合.与所有Kubernetes对象一样,使用YAML (首选)或JSON 定义服务.服 ...
- ThinkPHP3.2.3中M()和D()的区别详解
在实例化的过程中,经常使用D方法和M方法, 区别在于:M方法实例化模型无需用户为每个数据表定义模型类,如果D方法没有找到定义的模型类,则会自动调用M方法. 通俗一点说:1.M实例化参数是数据库的表名, ...
- vue---import的几种表现形式
在使用vue开发项目的时候,很多使用会import很多模块,或者组件,下面说下import的几种表现形式: 例如:我在 src / api / table.js import request from ...
- SSO(singlesignon)单点登录
技术实现机制: 当用户第一次访问应用系统1的时候,因为还没有登录,会被引导到认证系统中进行登录:根据用户提供的登录信息,认证系统进行身份效验,如果通过效验,应该返回给用户一个认证的凭据--ticket ...
- git 设置tracking information
There is no tracking information for the current branch.Please specify which branch you want to merg ...