Linq List of Lists to Single List

Linq list of lists to single list

You want to use the SelectMany extension method.

var residences = details.SelectMany(d => d.AppForm_Residences).ToList();

Converting a list of lists into a single list using linq

You can do:

var allTrackAreasCombined = allTrackAreas.SelectMany(t => t).ToList();

Linq get specific item from list of lists

Your code doesn't compile and have some erros. You can use SelectMany:

var actualRes = example.Children
.SelectMany(x => x.Elements)
.FirstOrDefault(x => x.Id == MyId);

Code with pre-populated examples:

Example example = new(){
Children = new()
};
for(int i=0; i < 10; i++){
Children c = new(){
Id = i.ToString(),
Elements = new()
};
example.Children.Add(c);
for(int j=0; j < 10; j++){
Element e = new(){
Id=(10*i+j).ToString()
};
c.Elements.Add(e);
}
}
string MyId="23";
var result1 = example.Children
.SelectMany(x => x.Elements)
.FirstOrDefault(x => x.Id == MyId);

How to flatten list of lists?

Use SelectMany:

var legalEntityIds = 
query.SelectMany(x => x.LegalEntities).Select(y => y.LegalEntityId).ToList();

or, using query syntax:

var legalEntityIds = (
from item in query
from legalEntity in item
select legalEntity.LegalEntityId
).ToList();

How to merge a list of lists with same type of items to a single list of items?

Use the SelectMany extension method

list = listOfList.SelectMany(x => x).ToList();

Is there better way to make list into list of lists C#

If you want Character.Items to be a List<item> instead of List<CharacterDto>.

First we have to change the object definition:

public class Character
{
public int Id {get;set;}
public string Name {get;set;}
public List<Item> Items {get;set;}
}

Then you can use Select to map the grouped CharacterDto to Item:

Items = y.Select(c=> new Item{ ItemName = c.ItemName, ItemId = c.Item }).ToList()

Using LINQ I have a list of lists, how do I select all objects that exist in every list?

list.Select (x => x.Item2 as IEnumerable<SomeObject>)
.Aggregate((x,y)=> x.Intersect(y))
.ToList();

Or, as Jeppe Stig Nielsen suggested (and I think it's much more elegant):

list.Select(x => x.Item2.AsEnumerable())
.Aggregate(Enumerable.Intersect)
.ToList();

Linq: List of lists to a long list

ybo's answer would have been my first response too. The query expression equivalent of this is:

var query = from a in computeAList()
from b in a.Alist
select b.C;

For the sake of completeness, the other answers in this thread are variations on the same theme.

From ybo (the exact same query, expressed as dot notation):

var query = listOfA.SelectMany(a => a.Alist, (a, b) => b.C);

From Ray Hayes (including the Where clause; I've reformatted slightly):

var query = listOfA.SelectMany(a => a.AList, (a, b) => b.C)
.Where(c => c.Length > 0);

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

This flattens your list and then does a distinct on it

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


Related Topics



Leave a reply



Submit