怎样写个类似这样的查询:students.where(s=>s.id in id_list)

海涵德 2013-07-25 07:07:17
怎样写个类似这样的查询:

List<int> id_list=new List<int>(){1,2,3,4,5};
rs=students.where(s=>s.id in id_list);

这个语法肯定是错误的,但是我不知道怎么写,就是要查出id在id_list中的学生。
...全文
315 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
海涵德 2013-08-20
  • 打赏
  • 举报
回复
class Student { public int id { get; set; } public string name {get;set;} } List<Student> students = new List<Student>() { new Student (){ id =9, name ="n9"} , new Student (){ id =1, name ="n1"}, new Student (){ id =2, name ="n2"}, new Student (){ id =3, name ="n3"}, new Student (){ id =4, name ="n4"}, new Student (){ id =5, name ="n5"}, new Student (){ id =6, name ="n6"}, new Student (){ id =7, name ="n7"}, new Student (){ id =8, name ="n8"}, }; List<int> ids = new List<int>() { 8, 2, 3, 4 }; var query1 = students.Where(x => ids.Any(y => x.id == y)); foreach (Student r in query1) { int id = r.id; } datagrid_students.ItemsSource = query.ToList<Student>();
haixinl2012 2013-08-20
  • 打赏
  • 举报
回复
四楼条件写错了
haixinl2012 2013-08-20
  • 打赏
  • 举报
回复
引用 5 楼 haixinl2012 的回复:
List<studern> listStuder=(from student in students where student.id.Contains(student.id)).toList<Student>(); List<studern> listStuder=new List<studern>(); for(int i=0;i<id_list.count;i++) { List<studern> listStu=(from student in students where student.id=id_list[i]).toList<Student>(); for(int j=0;j<listStu.count;i++) { listStuder.add(listStu[j]); } }
haixinl2012 2013-08-20
  • 打赏
  • 举报
回复
List<studern> listStuder=(from student in students where student.id.Contains(student.id)).toList<Student>(); List<studern> listStuder=new List<studern>(); for(int i=0;i<id_list.count;i++) { List<studern> listStu=(from student in students where student.id.Contains(student.id)).toList<Student>(); for(int j=0;j<listStu.count;i++) { listStuder.add(listStu[j]); } }
pan512 2013-08-08
  • 打赏
  • 举报
回复
rs=students.where(s=>id_list.Contains(s.id)).ToList();
afacat阿法猫 2013-08-03
  • 打赏
  • 举报
回复
用子查询吧!!!!
华尔兹 2013-07-28
  • 打赏
  • 举报
回复
rs=students.where(s=>id_list.Contains(s.id));
Bonjour-你好 2013-07-25
  • 打赏
  • 举报
回复

    public class student
    {
        public Int32 ID { get; set; }
        public String Name { get; set; }
    }

var rs = students.Join(id_list, x => x.ID, y => y, (x, y) => new { mID = x.ID, mName = x.Name });

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧