列等于列表项的Lambda表达式 [英] Lambda expression where column is equal to list items

查看:65
本文介绍了列等于列表项的Lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的一般结果有不同的列,包括 id 列.我也有一个List,其中有一组 id .我想得到List item(id)与 Id 列值匹配的结果.

I have a general result having different columns including id column. I also have a List which has a set of id's. I want to get result where List item(id) matches the Id column value.

我试图循环执行此操作:

I tried doing this in a loop:

foreach(int Uid in idList)
{
    queryResults = queryResults.Where(security => security.id== Uid);
}  

但这给了我 queryResults 中的一条记录,该记录是List中最后一个 Uid 的记录.我想要的是,列表中所有 Uid 的记录都应该在 queryResults 中.

But this gives me single record in queryResults which is for the last Uid in List. What I want is, records for all Uid's in List should be there in queryResults.

推荐答案

您需要将每个项目的ID与存储在idList中的ID进行匹配.这可以通过将queryResult上使用的Where-扩展名与idList上使用的Contains-方法相结合来实现:

You will need to match the id of every item to the ids stored in your idList. This can be achieved by means of the Where-extension used on your queryResult in combination with the Contains-method used on the idList:

var idList = new List<int>{1, 2, 3, 4} // This is your list holding the ids
var result = queryResult.Where(security => idList.Contains(security.SecuritiesId));

这将检查queryResult的每个项目,其SecuritiesId是否包含在包含相关ID的列表中.

This will check for every item of the queryResult whether its SecuritiesId is contained in the list containing the relevant ids.

这篇关于列等于列表项的Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆