Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中创建表达式树以使用LINQ.Expressions选择多个记录_C#_Sql Server_Linq - Fatal编程技术网

C# 在c中创建表达式树以使用LINQ.Expressions选择多个记录

C# 在c中创建表达式树以使用LINQ.Expressions选择多个记录,c#,sql-server,linq,C#,Sql Server,Linq,对于以下示例学生列表,我希望使用表达式树从列表中选择多个记录以生成动态LINQ查询 - 1 | John | 13 - 2 | Steve | 15 - 3 | Bill | 18 - 4 | Ram | 12 - 5 | Ron | 21 用于选择单个记录 林克: var studentsData = studentList.Where(s=>s.StudentID == 2).AsQueryable(); 同样,我需要创建表达式树,以便从值列表中选择多个记录

对于以下示例学生列表,我希望使用表达式树从列表中选择多个记录以生成动态LINQ查询

 - 1 | John  | 13
 - 2 | Steve | 15
 - 3 | Bill  | 18
 - 4 | Ram   | 12
 - 5 | Ron   | 21
用于选择单个记录

林克:

var studentsData = studentList.Where(s=>s.StudentID == 2).AsQueryable();
同样,我需要创建表达式树,以便从值列表中选择多个记录

例如SQL查询:

样本输出:

示例表达式树代码:


我可以创建用于选择单个记录的表达式代码。但是我无法使用数据列表中的多个记录来选择值。请帮助我使用表达式语句选择多个值

我认为试图用表达式修复它是一项非常艰巨的工作。这些功能已经可以通过现有方法实现。这使得你的解决方案更加简单-

你可以试试这个:

// 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。。声明。

我认为试图用表达式修复它是一项非常艰巨的工作。这些功能已经可以通过现有方法实现。这使得你的解决方案更加简单-

你可以试试这个:

// 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 };
您可以为此创建一个表达式树,但这是一种困难的做法。更简单的方法是创建一个ID列表:

var ids = new List<int> { 2, 3, 4 };
// 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));
var ids = new List<int> { 2, 3, 4 };
var filtered = studentList.Where(x => ids.Contains(x.StudentID));