LINQ to SQL Select Distinct by Multiple Columns and return entire entity

前端 未结 1 1598
予麋鹿
予麋鹿 2020-12-20 19:18

I am working with a third party database and need to select a distinct set of data for the specific market that I am looking into. The data is the same for each market, so i

相关标签:
1条回答
  • 2020-12-20 19:47

    You could use group by with the properties that you want to be distinct, then select the first item of each group:

    determinantData = (from x in dbContext.Datas
                       where x.Bar.Name.Equals(barName) &&
                             x.Something.Name.Equals(someName) &&
                             FooIds.Contains(x.Foo.Id) &&
                             x.Date >= startDate && 
                             x.Date <= endDate
                       group x by new { x.Foo, x.Bar, x.Something } into market 
                       select market).Select( g=> g.First()); 
    
    0 讨论(0)
提交回复
热议问题