gpt4 book ai didi

c# - 在 LINQ 查询的 where 子句中传递 int 数组

转载 作者:太空狗 更新时间:2023-10-29 20:54:50 25 4
gpt4 key购买 nike

有一个类

Public class Student
{
int id;
int rollNum;
String Name;
}

一个方法

Public List<Student> GetAllStudents()
{
var studentList = GetMeAll(); // Simple select * query to get all students
//unfortunately i can't make changes in this method so have to write a linq query to //filter data.
//getting int array ids correctly from some method
var filterList = FilterStudentsByID(ids,StudentList);
}

我想写一个方法,将一个int数组作为输入并返回一个List

Public List<Student> FilterStudentsByID(int[] ids,List<student> studentList)
{
//There should be a linq query , but I am new to linq so don't know how to write this.
Var list = from studentList in ..... don't know much;
}

编写查询的任何帮助。谢谢大家的回答,我也在寻找维护具有大量记录的表演??如果您能给我添加关于查询将在内部工作的简单解释,那就太好了??

最佳答案

如果您要从列表中找到那些 ID 存在于 ids 数组中的学生,那么:

var list = from s in stundentList
where ids.Contains(s.ID)
select s;

关于c# - 在 LINQ 查询的 where 子句中传递 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com