c# - 如何使用 C# 在 Linq 查询中设置动态 Where 子句

标签 c# asp.net

我有这段代码:-

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/

相关文章:

c# - 搜索和替换拆分为多个的占位符 <w :t>-Elements

c# - 无法将字符串转换为 int。来自 XML 元素的数据。 C#

c# - "Collection was modified"尝试枚举包中的文件时出错

c# - 带有 CosmosDBTrigger 的 Azure 函数似乎不是由 upsert 触发的

asp.net - bool 类型上的xml序列化错误

c# - 为什么将 DllImport 返回值中的 char[] 替换为 IntPtr 或 StringBuilder 会导致我的程序不再找到正确的入口点?

c# - "Bad Version of provider."使用 RSACryptoServiceProvider 加载公钥时

c# - 如何进行 WCF 序列化以在通用字典上保留非默认比较器?

c# - 为特定行禁用 jsgrid 的编辑和删除按钮

c# - 如何在同一个网站调用一个webservice?