Suppose I have two Lists<myObject> where myObject consists of the two properties

Id (of type Int) and

Value (of type Double)

I need to get a list out of these two lists that is made of (anonymous) objects like this:

Id, [Double value from List 1], [Double value from List 2] 

So if for a given Id both lists contain a value, it should look like this example:

12, 21.75, 19.87 

If one list does not contain an object with an Id that is present in the other list, the value should be null:

15, null, 22.52 

How can I achieve that? Update: I know how I could get such a list, of course, but I'm looking for the most performant way to do it, preferrably by using some witty Linq magic.

2

3 Answers

Not sure how optimized this is, but should suit your needs - Assuming I understood what you wanted:

var enumerable1 = new[] { new {Id = "A", Value = 1.0}, new {Id = "B", Value = 2.0}, new {Id = "C", Value = 3.0}, new {Id = "D", Value = 4.0}, new {Id = "E", Value = 5.0}, }; var enumerable2 = new[] { new {Id = "A", Value = 6.0}, new {Id = "NOT PRESENT", Value = 542.23}, new {Id = "C", Value = 7.0}, new {Id = "D", Value = 8.0}, new {Id = "E", Value = 9.0}, }; var result = enumerable1.Join(enumerable2, arg => arg.Id, arg => arg.Id, (first, second) => new {Id = first.Id, Value1 = first.Value, Value2 = second.Value}); foreach (var item in result) Console.WriteLine("{0}: {1} - {2}", item.Id, item.Value1, item.Value2); Console.ReadLine(); 

The resulting output would be something akin to:

A: 1 - 6 C: 3 - 7 D: 4 - 8 E: 5 - 9 

Don't really see why you would want null values returned, unless you absolutely need to (Besides, double is not-nullable, so it would have to be the resulting combined entry that would be null instead).

1

The requirement is slightly unclear. Do you want a Cartesian product or a join on Id? If the latter, then this should work:

var result = from l1 in list1 join l2 in list2 on l1.Id equals l2.Id select new {l1.Id, Value1 = l1.Value, Value2 = l2.Value}; 

If you actually want a full outer join, see this.


**Let say tempAllocationR is list 1 and tempAllocationV is List2 **


var tempAllocation = new List<Object>(); if (tempAllocationR.Count > 0 && tempAllocationV.Count > 0) { foreach (TempAllocation tv in tempAllocationV) { var rec = tempAllocationR.FirstOrDefault(tr => tr.TERR_ID == tv.TERR_ID && tr.TERR == tv.TERR && tr.Team == tv.Team); if (rec != null) { rec.Vyzulta = tv.Vyzulta; } else { tempAllocationR.Add(tv); } } tempAllocation = tempAllocationR; }} 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy