Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Select Multiple value

Tags:

c#

linq

My code is as below , the usage of this code is to merge 2 list together. and replace its value from one to another.

(from L1 in List1
         join L2 in List2
         on L1.itemID equals L2.itemID
         select  L1.itemName= L2.itemName).ToArray();

The code above work perfectly but only for selecting a single attribute which is itemName , how should I write the code if I want to select more than 1 value ,

e.g

(from L1 in List1
     join L2 in List2
     on L1.itemID equals L2.itemID
     select  {L1.itemName= L2.itemName , L1.ItemQuantity = L2.Quatity}).ToArray();
like image 571
abc cba Avatar asked Mar 28 '13 04:03

abc cba


Video Answer


1 Answers

You could directly use the property names, as shown below.

The returning array would contain objects with the same Properties itemID and itemName.

        var outp = (from L1 in List1
                    join L2 in List2
                    on L1.itemID equals L2.itemID
                    select new { L1.itemID, L2.itemName }).ToArray();

Sample Output:

enter image description here

like image 123
jacob aloysious Avatar answered Oct 03 '22 16:10

jacob aloysious