0

I've the following data set that has to be combined and get a new list of object:

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 = "A", Value = 16.0},
    new {Id = "A", Value = 20.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},
    new {Id = "E", Value = 10.0},
};

Expected Output:

A - 43
B - 2
C - 3
D - 4
E - 24

For single set of data, I used the following and the data set as follows:

var result = enumerable1.Join(enumerable2, arg => arg.Id, arg => arg.Id,
(first, second) => new {Id = first.Id, Value1 = first.Value, Value2 = second.Value});

Data Set:

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},
};

But for the repeated values in list 2, how can I merge and sum all repeated values with list 1?

1 Answer 1

1

One approach is to filter out missing from the second collection, concatenate and use GroupBy:

var unique = enumerable1.Select(e => e.Id).ToHashSet(); 
var list = enumerable1
    .Concat(enumerable2.Where(e => unique.Contains(e.Id)))
    .GroupBy(e => e.Id)
    .Select(g => new { Id = g.Key, Value = g.Sum(e => e.Value) })
    .ToList();

A for join - I would recommend to switch to query syntax to perform outer join (otherwise B will be missing). Something along this lines:

var list1 = (from e1 in enumerable1
        join e2 in enumerable2 on e1.Id equals e2.Id into j
        from outer in j.DefaultIfEmpty()
        group new { e1.Id, e1.Value, Value2 = outer?.Value ?? 0 } 
             by new { e1.Id, e1.Value } // light hack for easier result calculation 
        into gr
        select new { gr.Key.Id, Value = gr.Key.Value + gr.Sum(e => e.Value2) }
    )
    .ToList();

Both approaches rely on the uniqueness of the Id in the first collection.

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

2 Comments

Why do you need to make use of ToHashSet and prefer Concat over Join ?
@SonerfromTheOttomanEmpire so I don't need to figure out the left outer join and I find it more intuitive.

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.