我现在有这样的东西,它只搜索 1 个 LabID
List<Expense> lstExpenses = (from l in ctx.Expenses
where l.datLab.Lab_ID == labID
select l).ToList<Expense>();
我如何返回 Lab_ID 在我的字典中的所有结果:
以下函数返回字典值:report.GetLabChildren(labID, "All");
在<short>,<string>
格式。我只想搜索 <short>
最佳答案
如果你想在 linq 中对对象进行过滤:
var query = ctx.Expenses.AsEnumerable()
.Where(expense => dictionary.ContainsKey(espense.Lab_Id));
如果你想要一些可以翻译到数据库的东西:
var ids = dictionary.Keys.ToList();
var query = ctx.Expenses
.Where(expense => ids.Contains(expense.Lab_Id));
请注意,这仅在您的字典中的键少于几千个时才有效,因为它被转换为 SQL IN 子句。如果您有更多,请将它们上传到临时表并连接两个表,或者拉出整个 Expenses
表并使用 linq to objects 版本。
关于c# - 使用 LINQ 获取字典中 ID 所在的所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18902093/