两个列表对象之间的Linq查询 [英] Linq query between two list objects

查看:84
本文介绍了两个列表对象之间的Linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象:

ObjectA
{
   string code;
   string country;
}

ObjectB
{
   string code;
   string otherstuff;
}

我有List<objectA>List<ObjectB>,我需要在List<ObjectB>中找到所有包含objectA.Code的对象. 但是无法在LINQ查询上实现它.

And I have List<objectA> and List<ObjectB> and I need to find all objects in List<ObjectB> which contains objectA.Code. But cannot manage to implement it on LINQ query.

推荐答案

听起来您正在尝试查找所有ObjectB实例,这些实例在任何List<ObjectA>值中都具有code值.如果是这样,请尝试以下

It sounds like you are trying to find all instances of ObjectB which have a code value present in any of the List<ObjectA> values. If so try the following

List<ObjectA> listA = ...;
List<ObjectB> listB = ...;
var all = listB.Where(b => listA.Any(a => a.code == b.code));

这篇关于两个列表对象之间的Linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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