2

I have two lists. Each list has a Name object and and a Value object. I want to loop through list1 and check if each list1 Name object is the same as the list2 Name object (the linq code below does this).

If they match, then I want the List1 Value to be set with the list2 Value How can this be done?

list1              list2
Name    Value      Name    Value
john    apple      John    orange
peter   null       Peter   grape

I need it to look like this:

list1              list2
Name    Value      Name    Value
john    orange     john    orange
peter   grape      peter   grape

Linq code:

var x = list1.Where(n => list2.Select(n1 => n1.Name).Contains(n.Name));
4
  • the regular expression match requirement is gone? Commented Aug 27, 2014 at 17:45
  • @IgorPashchuk: He removed it after nobody answered it. I added a comment to the accepted answer just before he removed it, but I think I was too late for him to notice. Commented Aug 27, 2014 at 17:51
  • @IgorPashchuk I removed it because I need to reword the question. I can put it back up in a couple of minutes Commented Aug 27, 2014 at 17:53
  • @IgorPashchuk I have the question reworded here: link Commented Aug 27, 2014 at 19:27

3 Answers 3

2

For filtering you can use LINQ, to set the values use a loop:

var commonItems = from x in list1
                  join y in list2
                  on x.Name equals y.Name
                  select new { Item = x, NewValue = y.Value };

foreach(var x in commonItems)
{
     x.Item.Value = x.NewValue;
}
Sign up to request clarification or add additional context in comments.

4 Comments

select { Item = x, NewValue = y.Value }; line does not work. x and y can't be found
you are missing new after select
@BenScotch: You could add a where clause to the first part of the linq query from x in list1.Where(item => regex.IsMatch(item.Value)) join y in list2 ... You would just have to set up the regex before the query and run the foreach at the end (how it is in this answer already). (I see you have now removed the regex part of your question and also the comment from this answer, but I'll leave this comment in case you still find it helpful).
@JasonDown I have the question reworded here: link
1

In one result, you can get the objects joined together:

var output= from l1 in list1
        join l2 in list2
        on l1.Name equals l2.Name
        select new { List1 = l1, List2 = l2};

And then manipulate the objects on the returned results. by looping through each and setting:

foreach (var result in output)
  result.List1.Value = result.List2.Value;

Comments

0

You are looking for a left join

var x = from l1 in list1
        join l2 in list2 on l1.Name equals l2.Name into l3
        from l2 in l3.DefaultIfEmpty()
        select new { Name = l1.Name, Value = (l2 == null ? l1.Value : l2.Value) };

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.