簡體   English   中英

在C#中創建表達式樹以使用LINQ.Expressions選擇多個記錄

[英]create Expression tree in c# to select multiple records using LINQ.Expressions

對於以下示例學生列表,我想使用表達式樹從列表中選擇多個記錄(以生成動態LINQ查詢)

 - 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; }
}

我可以創建用於選擇單個記錄的表達式代碼。 但是我無法使用數據列表中的多個記錄來選擇值。請您幫我使用表達狀態來選擇多個值。

我認為嘗試用表達式解決問題是一件很艱辛的事情。 這些功能已經可以通過現有方法實現。 (這使您的解決方案更加簡單) ;-)

您可以嘗試以下方法:

// 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));

ids數組將轉換為IN (..)語句。

可以為此創建一個表達式樹,但是這樣做很困難。 更簡單的是只創建一個ID列表:

var ids = new List<int> { 2, 3, 4 };

並使用:

var filtered = studentList.Where(x => ids.Contains(x.StudentID));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM