Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 在LINQ查询的where子句中传递int数组_C#_Linq_Generics - Fatal编程技术网

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

C# 在LINQ查询的where子句中传递int数组,c#,linq,generics,C#,Linq,Generics,有一节课 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

有一节课

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);
}
公共列表GetAllStudents()
{
var studentList=GetMeAll();//简单选择*查询以获取所有学生
//不幸的是,我无法对此方法进行更改,因此必须编写一个linq查询来//过滤数据。
//从某些方法正确获取int数组ID
var filterList=filterstudentsbyd(id,StudentList);
}
我想写一个以int数组作为输入并返回列表的方法

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;
}
公共列表过滤器studentsbyid(int[]id,List studentList)
{
//应该有一个linq查询,但我是linq新手,所以不知道如何编写这个。
Var list=来自……中的studentList,不太了解;
}

任何关于编写查询的帮助。谢谢大家的回答,我也在寻找保持大名单记录的表演??如果您能为我添加查询的简单解释将非常有用???

如果您希望从列表中找到id出现在
ids
数组中的学生,那么:

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


我认为已经给出的答案足够快,但万一测试后你发现速度太慢:你可以在搜索之前尝试将学生列表转换成字典

您应该在仔细的计时测试之后才使用它,因为在大多数情况下它可能会慢得多

public static List<Student> FilteredStudents(int[] targetIds)
{
    var allStudents = GetAllStudents().ToDictionary(student => student.id);
    var result = new List<Student>();

    foreach (int id in targetIds)
    {
        Student student;

        if (allStudents.TryGetValue(id, out student))
        {
            result.Add(student);
        }
    }

    return result;
}
publicstaticlist-FilteredStudents(int[]targetIds)
{
var allStudents=GetAllStudents().ToDictionary(student=>student.id);
var result=新列表();
foreach(targetId中的int id)
{
学生;
if(allStudents.TryGetValue(id,out student))
{
结果。添加(学生);
}
}
返回结果;
}

您能解释一下这在内部是如何工作的吗?比如,如果列表很大,它会影响性能吗?看起来是O(N*M),其中N=学生人数,M=ID人数。但我认为你做得再好不过了。@ankur,LINQ在内部使用迭代,所以它会根据id数组检查每个学生id。这在与数据库交互时非常有用,因为这将创建一个
Select*from表,其中ID位于(1,2,3)
类查询中。不幸的是,在这种情况下,它不会在数据库上执行,因为“studentList”是一个列表,而不是IQueryable。@只有另一个用户您可能知道,是的,这就是问题中的每个学生是否都有唯一的ID?另外:ID列表是否会很长(比如说,超过学生列表大小的5%)?在进行搜索之前,只是想弄清楚是否值得创建一个按ID排序的学生列表。是的,它可能是大小的5%左右
public static List<Student> FilteredStudents(int[] targetIds)
{
    var allStudents = GetAllStudents().ToDictionary(student => student.id);
    var result = new List<Student>();

    foreach (int id in targetIds)
    {
        Student student;

        if (allStudents.TryGetValue(id, out student))
        {
            result.Add(student);
        }
    }

    return result;
}