我正在使用动态 Linq 进行通用搜索。我有 Id 列表:
List<int> idList = new List<int> { 1, 5, 6};
Run Code Online (Sandbox Code Playgroud)
在普通的 Linq 中,我会写:
q = q.Where(a => idList.Contains(a.MyId));
Run Code Online (Sandbox Code Playgroud)
但是现在我必须使用,System.Linq.Dynamic因为我事先不知道该列的名称。
string someId = "CustomId";
q = q.Where("@0"+ ".Contains(" + someId + ")", idList.ToArray());
Run Code Online (Sandbox Code Playgroud)
但这给出了错误:
“'Int32' 类型中不存在适用的方法 'Contains'”
我怎样才能做到这一点?
是否有一些Contains为dynamicLinq 或其他方式实现的扩展库。
我有一个要在其中查询变量列的表。喜欢:
private void query(string column, string value) {
using (var db = new myDB()) {
var s1 = (from c in db.Components
where (**column** == **value**)
select new {c.id, **column**});
}
}
Run Code Online (Sandbox Code Playgroud)
可以说我想寻找供应商,那会是:
var s1 = (from c in db.Components
where (c.supplier == "abc")
select new {c.id, c.supplier});
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以将列名作为变量传递?