0

Created a class

 public class DBClass: IDisposable
    {
        public string Name { get; set; }
        public int Score { get; set; }
    }

Used this class to create list of the class.

        List<DBClass> dbc1 = new List<DBClass>();
        List<DBClass> dbc2 = new List<DBClass>();

I have added two different set of data to the list. note : This is just an example

        dbc1.Add(SetDBClass("A", 100));
        dbc1.Add(SetDBClass("B", 100));
        dbc1.Add(SetDBClass("C", 100));

        dbc2.Add(SetDBClass("B", 200));
        dbc2.Add(SetDBClass("C", 200));
        dbc2.Add(SetDBClass("D", 200));

I need a List<DBClass> object with the above two objects merged but in the below fashion.

A - 100 B - 300 C - 300 D - 200

How can the above be achieved by LINQ or any-other way ?

If you want to exclude the same data from both list then do as follows :

ParentDB.Where(b => !((from x in mainDataSource select x.Name).Contains(b.Name))).ToList();
Pradip
  • 1,507
  • 11
  • 28

1 Answers1

11
from x in dbc1.Concat(dbc2)
group x by x.Name into g
select new DBClass(g.Key, g.Sum(x => x.Score)) 
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • This worked. But DBClass doesnot have any constructor. I changed a couple of part. Work like charm thanks :) – Pradip Jan 07 '14 at 17:54