• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

在C#中创建的表达式树使用LINQ.Expressions

c/c++ 来源:Vignesh Ammasi 3次浏览

对于下面的示例学生名单我想选择使用表达式树列表中多条记录,选择多个记录(生成动态LINQ查询)在C#中创建的表达式树使用LINQ.Expressions

- 1 | John | 13 
- 2 | Steve | 15 
- 3 | Bill | 18 
- 4 | Ram | 12 
- 5 | Ron | 21 

对于选择单条记录

SQL Query: 
select * from studentList where StudentID = 2 

LINQ:

var studentsData = studentList.Where(s=>s.StudentID == 2).AsQueryable(); 

我同样需要创建的表达式树从值

例如SQL查询的列表中选择多个记录:

From the list of IDs I need create expression for selecting records  
select * from studentList where StudentID in (2,4,3) 

输出样本:

- 2 | Steve 
- 4 | Ram 
- 3 | Bill 

样品表达树代码:

using System; 
using System.Linq; 
using System.Linq.Expressions; 
using System.Collections.Generic; 

public class Program 
{ 
public static void Main() 
{ 
    IList<Student> studentList = new List<Student>() { 
     new Student() { StudentID = 1, StudentName = "John", Age = 13 } , 
     new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } , 
     new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } , 
     new Student() { StudentID = 4, StudentName = "Ram" , Age = 12 } , 
     new Student() { StudentID = 5, StudentName = "Ron" , Age = 21 } 
    }; 
    var studentwithLinQ = studentList.Where(s=>s.StudentID == 2); 
    foreach(var stu in studentwithLinQ) 
    Console.WriteLine("{0} | {1}",stu.StudentID, stu.StudentName); 
    ParameterExpression pe = Expression.Parameter(typeof(Student), "s"); 
    MemberExpression me = Expression.Property(pe, "StudentID"); 
    int id = 2; 
    ConstantExpression constant = Expression.Constant(id, typeof(int)); 
    BinaryExpression body = Expression.Equal(me, constant); 
    Expression predicateExpression = Expression.Lambda(body, pe); 
    var sourcequery = studentList.AsQueryable(); 
    Expression sourceExpression = Expression.Convert(Expression.Constant(sourcequery), typeof(IQueryable<Student>)); 
    Expression filterExpressionExpression = Expression.Constant(predicateExpression); 
    var queryExpression = Expression.Call(typeof(Queryable), "Where", new Type[] { typeof(Student)}, sourceExpression,filterExpressionExpression); 
    sourcequery = Expression.Lambda(queryExpression).Compile().DynamicInvoke() as IQueryable<Student>; 
    Console.WriteLine("sourceExpression: {0}", sourcequery); 
    var studentWithExpression = sourcequery; 
    foreach(var stu in studentWithExpression) 
    Console.WriteLine("{0} | {1}",stu.StudentID, stu.StudentName); 
    } 
} 

public class Student{ 
    public int StudentID { get; set; } 
    public string StudentName { get; set; } 
    public int Age { get; set; } 
} 

我可以创建一个用于选择单个记录的表达式代码。但是我无法使用datalist中的多条记录选择值。请帮助我使用表达式条件选择多个值。

===========解决方案如下:

我认为试图用表达式来修复它是一个地狱般的工作。这些功能已经可以通过现有的方法实现。 (这使您的解决方案更简单) ;-)

你可以试试这个:

// your original set 
IList<Student> studentList = new List<Student>() { 
    new Student() { StudentID = 1, StudentName = "John", Age = 13 } , 
    new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } , 
    new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } , 
    new Student() { StudentID = 4, StudentName = "Ram" , Age = 12 } , 
    new Student() { StudentID = 5, StudentName = "Ron" , Age = 21 } 
}; 

// create a subselection of ids only into an array(or list) of the id's only. 
// results in `int[] { 1, 2, 3, 4, 5 };` 
var ids = studentList.Select(student => student.StudentID).ToArray(); 

// use it on the studentList. 
var studentwithLinQ = studentList.Where(s => ids.Contains(s.StudentID)); 

的ID阵列将被转换为IN (..)声明。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)