13

假设我有一个学生表,我想显示 ID 为 1 的学生。

SELECT *
FROM STUDENT ST
WHERE ST.ID = 1

这就是我在 Linq 中实现这一点的方式。

StudentQuery = from r in oStudentDataTable.AsEnumerable()
                                     where (r.Field<int>("ID") == 1)
                                     select r;
            oStudentDataTable = StudentQuery.CopyToDataTable();

但是如果我想显示这些 id 为 1,2,3,4,5 的学生怎么办..

SELECT *
FROM STUDENT ST
WHERE ST.ID IN (1,2,3,4,5)

如何在 Linq 中实现这一点?

4

3 回答 3

20

采用.Contains

var list = new List<int> { 1, 2, 3, 4, 5 };

var result = (from r in oStudentDataTable.AsEnumerable()
              where (list.Contains(r.Field<int>("ID"))
              select r).ToList();
于 2012-10-12T12:56:47.797 回答
4

尝试IEnumerable.Contains

var list = new List<int>(){1,2,3,4,5};
StudentQuery = from r in oStudentDataTable.AsEnumerable()
                                 where (list.Contains(r.Field<int>("ID")))
                                 select r;
        oStudentDataTable = StudentQuery.CopyToDataTable();
于 2012-10-12T12:56:34.293 回答
3

也试试这个:

var list = new List<int> { 1, 2, 3, 4, 5 };

List<StudentQuery> result = (from r in oStudentDataTable.AsEnumerable()
              where (list.Contains(r.Field<int>("ID"))
              select new StudentQuery
              { /*
                .Your entity here
                .
                */
              }).ToList<StudentQuery>();
于 2012-10-17T12:32:57.423 回答