0

Using C# with Lambda(or some method), how can I merge different informations(from columns) of the same intituition name ? They can have some common and some different fields.

See a example below: enter image description here

Nalali
  • 70
  • 1
  • 5

2 Answers2

0

Have you tried the Group By instruction ?

Here is an other stackoverflow about this : Group by in LINQ

Community
  • 1
  • 1
Bagriel
  • 54
  • 6
0
    {
        List<Instituition> Instituitions = new List<Instituition>() { 
        new Instituition { Name = "Name1", Alias = "",Area="" },
        new Instituition { Name = "Name1", Alias = "Alias1",Area="" },
        new Instituition    { Name = "name2", Alias = "Audi" ,Area="444"},
         new Instituition { Name = "Name1", Alias = "",Area="Garden" }};


        var results = Instituitions.GroupBy(i =>  i.Name,
                           i => i.Alias,
                           (key, g) => new
                           {
                               Name = key,
                               Alias = g.Where(a =>a != "").FirstOrDefault(),
                               Area = Instituitions.Where(a=>a.Name == key && a.Area != "").Select(a=>a.Area).FirstOrDefault()
                           }                             

                          );


    }

    class Instituition
    {
        internal string Name;
        internal string Alias;
        internal string Area;
    }
Nalali
  • 70
  • 1
  • 5