繁体   English   中英

使用LINQ获取所有结果,其中字典中的ID

[英]Using LINQ to get all results where ID in dictionary

我现在有这样的东西,它只搜索1个LabID

List<Expense> lstExpenses = (from l in ctx.Expenses
                             where l.datLab.Lab_ID == labID
                             select l).ToList<Expense>();

我如何返回Lab_ID在字典中的所有结果:

followin函数返回一个字典值: 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版本。

我不确定您要问什么,但是您似乎在寻找ContainsKey方法:

var values = report.GetLabChildren(labID, "All");

List<Expense> lstExpenses = (from l in ctx.Expenses
                             where values.ContainsKey(l.datLab.Lab_ID)
                             select l).ToList<Expense>();

我不确定,但是有可能无法在LINQ to Entities上使用。 如果发生,请使用以下命令:

List<Expense> lstExpenses = (from l in ctx.Expenses
                             where values.Keys.Contains(l.datLab.Lab_ID)
                             select l).ToList<Expense>();
List<Expense> lstExpenses = (from l in ctx.Expenses
                      where report.GetLabChildren(labID, "All").Keys.Contains(i.Lab_ID)
                      select l).ToList<Expense>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM