简体   繁体   中英

How to join a list of lists in a unique list using Linq

I have an List of objects that have a List of long . I need to get all long of all objects and join it to a unique list. How to do this using System.Linq ?

This is my code actually (with this code I get a List of List of long )

var result = LIST1.Select(x => x.LIST2.Select(y => y.Id).ToList()).Tolist();

这会使您的列表变得平坦,然后对其进行区分

LIST1.SelectMany(a => a.LIST2.Select(b => b.Id)).Distinct();

使用selectMany而不是Select

var result = LIST1.SelectMany(x => x.LIST2.Select(y => y.Id)).Tolist();

Just dont get it if you need to join the lists of objects or you need to union all, but for join you could use:

var result = (from Item1 in LIST1
             join Item2 in LIST2
             on Item1.Id equals Item2.Id
             select new { Item1, Item2 }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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