动态LINQ查询
来源:互联网 发布:中国信息数据安全领域 编辑:程序博客网 时间:2024/06/09 21:03
//构造Student数组
Student[] StudentArrary = new Student[3]
{
new Student(){Name="王清培", Age=24, Sex="男", Address="江苏南京"},
new Student(){Name="陈玉和", Age=23, Sex="女", Address="江苏盐城"},
new Student(){Name="金源", Age=22, Sex="女", Address="江苏淮安"}
};
ParameterExpression parameter = Expression.Parameter(typeof(Student), "stu");//表示二元运算符的左边参数名称
//表示"stu"参数的"stu.Name"中的Name属性,Name属性必须是反射获取的元数据才行,这样框架就才可以找到它
MemberExpression property = Expression.MakeMemberAccess(parameter, typeof(Student).GetMember("Name")[0]);
//表示常量值
Console.WriteLine("请输入要查询人的名称:");
ConstantExpression name = Expression.Constant(Console.ReadLine());//从用户输入流中读取值
BinaryExpression binary = Expression.MakeBinary(ExpressionType.Equal, property, name);//拼接==运算符的左边、右边
//完整的表达式是Lambda才对
LambdaExpression lambda = Expression.Lambda(binary, parameter);
//重要的就在这里,我们将完整的Lambda表达式直接变成可以执行的委托
Func<Student, bool> wheredelegate = lambda.Compile() as Func<Student, bool>;
//将编译后的可执行委托直接放入Where方法中执行
var list2 = StudentArrary.AsQueryable<Student>().Where(wheredelegate);
foreach (var i in list2)
{
Console.WriteLine("查询列表:");
Console.WriteLine("姓名:{0},年龄:{1},地址:{2}", i.Name, i.Age, i.Address);
}
Console.ReadLine();
Student[] StudentArrary = new Student[3]
{
new Student(){Name="王清培", Age=24, Sex="男", Address="江苏南京"},
new Student(){Name="陈玉和", Age=23, Sex="女", Address="江苏盐城"},
new Student(){Name="金源", Age=22, Sex="女", Address="江苏淮安"}
};
ParameterExpression parameter = Expression.Parameter(typeof(Student), "stu");//表示二元运算符的左边参数名称
//表示"stu"参数的"stu.Name"中的Name属性,Name属性必须是反射获取的元数据才行,这样框架就才可以找到它
MemberExpression property = Expression.MakeMemberAccess(parameter, typeof(Student).GetMember("Name")[0]);
//表示常量值
Console.WriteLine("请输入要查询人的名称:");
ConstantExpression name = Expression.Constant(Console.ReadLine());//从用户输入流中读取值
BinaryExpression binary = Expression.MakeBinary(ExpressionType.Equal, property, name);//拼接==运算符的左边、右边
//完整的表达式是Lambda才对
LambdaExpression lambda = Expression.Lambda(binary, parameter);
//重要的就在这里,我们将完整的Lambda表达式直接变成可以执行的委托
Func<Student, bool> wheredelegate = lambda.Compile() as Func<Student, bool>;
//将编译后的可执行委托直接放入Where方法中执行
var list2 = StudentArrary.AsQueryable<Student>().Where(wheredelegate);
foreach (var i in list2)
{
Console.WriteLine("查询列表:");
Console.WriteLine("姓名:{0},年龄:{1},地址:{2}", i.Name, i.Age, i.Address);
}
Console.ReadLine();
0 0
- Linq动态条件查询
- linq 动态查询
- linq 动态查询
- LINQ动态组合查询
- linq - 动态生成查询
- linq 动态查询
- 动态LINQ查询
- 使用LINQ动态查询库
- linq中动态模糊查询
- linq动态多条件查询
- LINQ to Entity 动态查询
- linq实现动态查询和模糊查询
- 动态LINQ (第一部分:使用LINQ动态查询库)
- Linq To SQL动态查询 之 IQueryBuilder
- 泛型通用动态查询(LinQ+Ajax)
- Linq动态查询之Expressions扩展PredicateExtensions
- linq 动态拼接查询条件 扩展方法
- LINQ to SQL语句之动态查询
- Apple Mach-O (Id) Error ios编译错误(file too small file)
- maven
- Laravel 下配置 Redis 让缓存、Session 各自使用不同的 Redis 数据库
- Android 开源项目整理
- 读书笔记 《第一行代码》 第四章 碎片相关
- 动态LINQ查询
- matlab中的信噪比
- sspanel进阶——修改属于自己的sspanel站点(一)
- 初音未来虚拟人物
- <C#> 泛型、委托和一些易混淆的定义(1)
- HTML5 video标签只有声音没有图像
- kernel 高精度tick hrtimer 学习笔记
- video.js API
- 【Debug】fatal error C1083: 无法打开预编译头文件