Reputation:
I have an array containing following elements
{Mark=90, Students={"Tom","Marry","Jack"}},
{Mark=50, Students={"Niko","Gary","David"}},
{Mark=70, Students={"John","Andy","Amy"}}
I want a Linq sentence to convert them to
{Mark=90, name="Tom"},
{Mark=90, name="Marry"},
{Mark=90, name="Jack"},
{Mark=50, name="Niko"},
{Mark=50, name="Gary"},
{Mark=50, name="David"},
{Mark=70, name="John"},
{Mark=70, name="Andy"},
{Mark=70, name="Amy"}
How could I do?
Upvotes: 2
Views: 1306
Reputation: 727047
Use SelectMany
:
data.SelectMany(
item => Students.Select(
student => new {Mark = item.Mark, name=student.Name}
)
);
Upvotes: 2
Reputation: 161002
You could project to an anonymous class (or a real class if you need the sequence outside of your current method):
var results = from x in myArray
from s in x.Students
select new { x.Mark, name = s };
Upvotes: 4