简体   繁体   English

用嵌套列表连接两个列表

[英]Joining two lists with nested lists

Trying to find a way to join two lists along with nested lists through LINQ or PLINQ. 试图找到一种通过LINQ或PLINQ将两个列表与嵌套列表结合在一起的方法。

I have List A , List B and wanted to join these two with their parent and child properties. 我有List AList B并希望将其父级和子级属性与这两者结合在一起。

I have tried the following 我尝试了以下

List<data> result = A.Where(o=>foo.Any(x=>x.headerid=o.headerid)) &&
o.details.Any(y=>foo.Any(z=>z.headerid=o.headerid && z.detailid=y.detailid)).ToList();

But not sure this is the right way of doing it. 但是不确定这是否是正确的方法。 Here the foo list is being scanned twice. 在此,foo列表将被扫描两次。 I wanted to have a join query to fix this be it lambda or join query. 我想要一个联接查询来解决此问题,无论是lambda还是联接查询。 Preferably join query. 最好加入查询。

class filterclass
{
   public string headerid;
   public string detailid;
}

class data 
{
  public string headerid;
  public string headername;
  public List<Detail> details;
  //...
}

class detail
{
  public string detailid;
  public string detailname;
  //...
}

List<data> A = new List<data>();
List<filterclass> foo = new List<filterclass>();

Now my need is to join A and foo with the fields like A.headerid = foo.headerid and A.detail.detailid = foo.detailid . 现在,我需要将A和foo与A.headerid = foo.headeridA.detail.detailid = foo.detailid类的字段A.detail.detailid = foo.detailid

Thing is A.detail is the nested list and I am not sure how to join it. 事情是A.detail是嵌套列表,我不确定如何加入它。 Not sure whether the FirstOrDefault or All will work in this case. 不确定在这种情况下FirstOrDefaultAll是否将起作用。 Also don't want to loop 也不想循环

Any help is appreciated and thanks in advance. 任何帮助表示赞赏,并在此先感谢。

I need to have a LINQ JOIN query. 我需要一个LINQ JOIN查询。

You can use the Concat Extension Method for joining lists: 您可以使用Concat扩展方法来加入列表:

var combinedList = list1.Concat(list2);

And for your specific example: 对于您的特定示例:

var combinedList = A.Concat(foo);

It creates an instance of IEnumerable<T> which will enumerate the elements of list1 and list2 in that order. 它创建IEnumerable<T>的实例,该实例将按该顺序枚举list1list2的元素。

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

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