0

I am using following command in sql server to get data:

Select distinct ConstituentGroupNameId,UserId,CreatedTime  from dbo.ConstituentRecords

But I am unable to implement it through entity framework.I am tring to get unique ConstituentGroupNameId and other field as well.

Rupak
  • 409
  • 1
  • 3
  • 18

1 Answers1

1

You can simply use groupby in Linq query. It can reach same result like T-SQL's distinct operate.


Group fields you want to distinct first, you can get the groups you create. Then you select the first of items in groups.

Finally, You get the distinct rows you filter.

Lambda expression :

var boo = ConstituentRecords
.GroupBy(o => new { o.ConstituentGroupNameId, o.UserId, o.CreatedTime } )
.Select(g=>g.First())
.ToList();

or

query expression :

var boo = (from v in ConstituentRecords
           group v by new { v.ConstituentGroupNameId, v.UserId, v.CreatedTime } into g
           select g.First()).ToList();
Vic
  • 758
  • 2
  • 15
  • 31