C# linq 中的简单选择查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12859536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:43:08 来源:igfitidea点击:
simple select query in linq
提问by Cute Bear
Lets say I have a student table and I want to display the student with ID 1.
假设我有一个学生表,我想显示 ID 为 1 的学生。
SELECT *
FROM STUDENT ST
WHERE ST.ID = 1
This is how I achive this in Linq.
这就是我在 Linq 中实现的方式。
StudentQuery = from r in oStudentDataTable.AsEnumerable()
where (r.Field<int>("ID") == 1)
select r;
oStudentDataTable = StudentQuery.CopyToDataTable();
but what if I want to display the students with these ids 1,2,3,4,5..
但是如果我想显示这些 ID 为 1,2,3,4,5.. 的学生怎么办?
SELECT *
FROM STUDENT ST
WHERE ST.ID IN (1,2,3,4,5)
How can I achieve this in Linq?
我如何在 Linq 中实现这一目标?
采纳答案by Darren
回答by Piotr Zierhoffer
Try IEnumerable.Contains:
尝试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();
回答by Arun Manjhi
Try this also :
也试试这个:
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>();

