LINQ查询的WHERE子句中传递int数组 [英] Pass int array in where clause of LINQ Query

查看:882
本文介绍了LINQ查询的WHERE子句中传递int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一类

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



方法

a method

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数组作为输入并返回的方法名单

I want to write a method that takes an int array as input and returns a 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;
}

在编写查询任何帮助。谢谢大家的回答,我也在寻找保持与记录的大名单中的表演?这将是巨大的,如果你可以加我查询的简单说明内部工作??

Any help in writing the query. Thank you all for Answers, I am also looking for maintain the performances with large list of records ?? It will be great if you can add me simple explanation of the query will work internally ??

推荐答案

如果你想找到这些学生的ID是存在于目录ID 数组,那么:

If you are looking to find those students from the list whose id's are present in the ids array then:

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

这篇关于LINQ查询的WHERE子句中传递int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆