1

I am trying to get distinct values by joining multiple tables. Can someone please tell me how to do the same in the below code?

Please see the sample code below:

var distinctValues = (from a in dataContext.A_Table
                      join b in dataContext.B_Table on a.EmpID equals b.EmpID
                      join c in dataContext.C_Table on b.SomeID equals c.ID
                      where a.IsActive == true
                            && a.ID == id
                      select new NewClass()
                                 {
                                    ID = c.ID,
                                    Name = c.Name
                                 }).ToList();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user972255
  • 1,828
  • 10
  • 32
  • 52

1 Answers1

1

Well this should work without many changes, though i would have wrote it entirely in expressions:

var distinctValues = (from a in dataContext.A_Table
                      join b in dataContext.B_Table
                      on a.EmpID equals b.EmpID
                      join c in dataContext.C_Table
                      on b.SomeID equals c.ID
                      where a.IsActive == true
                      && a.ID == id
                      select new NewClass()
                      {
                          ID = c.ID,
                          Name = c.Name
                      }).ToList()
                      .GroupBy(x=>new {ID = x.ID,Name = x.Name})
                      .Select(x=>new {ID = x.Key.ID,Name = x.Key.Name});
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
  • I think the above code returns only the distinct Names but I need distinct objects of NewClass (i.e. with ID & Name columns). Can you please tell me how to return distinct ID & Name in the form of NewClass objects? – user972255 Aug 07 '13 at 20:26
  • Then you should change Distinct with a GroupBy. Check my update. – Giannis Paraskevopoulos Aug 07 '13 at 20:36