LINQ到SQL SELECT DISTINCT按多列,并返回整个实体 [英] LINQ to SQL Select Distinct by Multiple Columns and return entire entity
问题描述
我正与第三方数据库和需要选择一组数据为特定的市场,我寻找到了截然不同的。该数据是相同的每个市场,所以它是多余的上拉这一切,我不想为C周围没有任何逻辑很难$ C $,因为我们正在与供应商来解决这个问题,但我们需要一个修复程序将与供应商的解决以及方式的数据库,目前,因为它可以thier修复前一段时间去住。
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 it is redundant to pull it all in, and I don't want to hardcode any logic around it as we are working with the vendor to fix the issue, but we need a fix that will work with the vendors fix as well as the way the database is currently as it could be some time before thier fix goes live.
我不被任何东西要分组,因为我想要得到的数据在最低水平,但我不希望任何冗余数据。我现在的查询类似于这样...
I do not want to group by anything as I want to get the data at the lowest level, but I don't want any redundant data. My current query looks similar to this...
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
select x).Distinct();
这并没有做什么,我所期望的。我想通过三个属性来选择不同的记载,说富
,酒吧
和东西
,但返回的整个对象。我怎样才能做到这一点使用LINQ?
This does not do what I expect. I would like to select the distinct records by three properties, say Foo
, Bar
, and Something
but return the entire object. How can I do this using LINQ?
推荐答案
您可以使用按
与你想成为独特的属性,然后选择第一项各组:
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());
这篇关于LINQ到SQL SELECT DISTINCT按多列,并返回整个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!