2011-12-24 126 views
2

的陣列我有包含數組以下元素要求一個LINQ操縱陣列

{Mark=90, Students={"Tom","Marry","Jack"}}, 
{Mark=50, Students={"Niko","Gary","David"}}, 
{Mark=70, Students={"John","Andy","Amy"}} 

我想一個LINQ句子將它們轉換爲

{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"} 

我怎麼能這樣做?

回答

4

你能預料到一個匿名類(或一個真正的類,如果您需要電流法外的順序):

var results = from x in myArray 
       from s in x.Students 
       select new { x.Mark, name = s }; 
2

使用SelectMany

data.SelectMany(
    item => Students.Select(
     student => new {Mark = item.Mark, name=student.Name} 
    ) 
); 
+0

它的工作原理也是如此。謝謝。 – Gqqnbig 2011-12-30 05:53:06