1

I have two arrayList here here:

List<ArrayList1> data = new List<ArrayList1>() {
    { id = 1, name = "John Doe", age = 20 },
    { id = 2, name = "Mae Doe", age = 17 },
    { id = 3, name = "Mark Smith", age = 35 },
    { id = 4, name = "Selena Smith", age = 15 },
    { id = 5, name = "Shane Doe", age = 26 },
    { id = 6, name = "Will Smith", age = 45 }
};
List<ArrayList2> data = new List<ArrayList2>() {
    { id = 1, address = "Singapore", phone_number = "123", store_name = "ABC" },
    { id = 4, address = "Japan", phone_number = "456", store_name = "DEF" },
    { id = 5, address = "Korea", phone_number = "789", store_name = "GHI" }
};

I want to join them, kind of left join in mysql so the result will be like this

List<ArrayListResult> data = new List<ArrayListResult>() {
    { id = 1, name = "John Doe", age = 20, address = "Singapore", phone_number = "123", store_name = "ABC" },
    { id = 2, name = "Mae Doe", age = 17 },
    { id = 3, name = "Mark Smith", age = 35 },
    { id = 4, name = "Selena Smith", age = 15, address = "Japan", phone_number = "456", store_name = "DEF" },
    { id = 5, name = "Shane Doe", age = 26, address = "Korea", phone_number = "789", store_name = "GHI" },
    { id = 6, name = "Will Smith", age = 45 }
};

The address, phone_number, store_name will be merged to List based on the matched id.

Is there easy way of merging 2 arrayList in c# based on matched id? Please help. Thanks!

Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54

1 Answers1

2

You're looking for this:

result =
(
    from d1 in data1
    join d2 in data2 on d1.id equals d2.id into gds
    from gd in gds.DefaultIfEmpty()
    select new ArrayListResult()
    {
        id = d1.id,
        name = d1.name,
        age = d1.age,
        address = gd?.address,
        phone_number = gd?.phone_number,
        store_name = gd?.store_name,
    }
).ToList();

That gives me:

results

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • @MichaelRandall - Can you please help me out by voting to reopen https://stackoverflow.com/questions/63866460/need-help-altering-an-algorithm-to-make-a-spiral-going-outward ? – Enigmativity Sep 13 '20 at 11:49