Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ selecting a list with a property that has multiple value

Tags:

c#

linq

I have this list which has the following value

id, name, List<Tag>

where Tag has 2 data, TagID and TagName

Now I want to use linq to select the list with this output

id1, name1, tagid1, tag1
id1, name1, tagid2, tag2
id1, name1, tagid3, tag3
id2, name2, tagid4, tag4

How to do this in linq?

like image 414
Rashid Avatar asked Jan 27 '23 08:01

Rashid


1 Answers

Use SelectMany:

list.SelectMany(item => item.Tags.Select(tag => new { item.Id, item.Name, tag.Id, tag.Tag }));
like image 107
Sergey Kalinichenko Avatar answered Jan 30 '23 07:01

Sergey Kalinichenko