6

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();
2
  • 1
    By using SelectMany maybe? Commented Mar 28, 2013 at 4:18
  • You are missing "new" between select and {, and the names to the left of the equal signs are new properties and not part of L1, thus should not be prefixed by L1. All in all, you want something like select new { ItemName1 = L1.itemName, ItemName2 = L2.itemName, Quantity = L2.Quantity } Commented Mar 28, 2013 at 4:49

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

Comments

1
(from L1 in List1
         join L2 in List2
         on L1.itemID equals L2.itemID
         select  new{Prop1 = L1.SomePropery,Prop2 = L1.SomeOtherProperty).ToArray();

OR unnamed -using default names

(from L1 in List1
         join L2 in List2
         on L1.itemID equals L2.itemID
         select  new{L1.SomePropery,L1.SomeOtherProperty).ToArray();

6 Comments

it is returning this error "Error 118 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access."
Did you replace my made up names with the real property names? SomeProperty etc..
yup , my code as follow select {L1.itemName= L2.itemName , L1.ItemQuantity = L2.Quatity}).ToArray();
You have to turn Li.itemName etc (all names on the left of the = sign) into a one word.. Like LiItemName or similar. They have to be valid c# property names
You can also leave them unnamed by like this new{ L1.SomePropery,L1.SomeOtherProperty}
|
0

If you wish to merge the two objects, you should first match corresponding objects to be joined, then go through the matched objects and copy the properties over. Don't use a LINQ query like that, that's not what it was designed for.

// pair off all the items in both lists
var matches =
    from item1 in List1
    join item2 in List2 on item1.ItemID equals item2.ItemID
    select new { item1, item2 };
foreach (var match in matches)
{
    // copy the properties over for each corresponding match
    match.item1.ItemName = match.item2.ItemName;
    match.item1.ItemQuantity = match.item2.Quantity;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.