我有这段代码:-
DataSet ds = new DataSet();
String s = "StudentID = 5 Or StudentID=6";
var result = from r in ds.table[0].AsEnumerable() where s.ToString() select r;
如何从中获取数据?
最佳答案
为什么不能使用 ID 列表?
//assuming you have text and that's the reason
var txtIDs = "5,6";
var IDs = txtIDs.Split(',').Select(s => int.Parse(s));
var rows = from r in ds.Tables[0].AsEnumerable()
where IDs.Any(id => r.Field<int>("ID")==id)
select r;
或在方法语法中:
var rows = ds.Tables[0].AsEnumerable()
.Where(r => IDs.Contains(r.Field<int>("ID")));
关于c# - 如何使用 C# 在 Linq 查询中设置动态 Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12871887/