[英]Entity Framework returning distinct records after join
Consider we have these two entities and one custom object : 考虑我们有这两个实体和一个自定义对象:
public class Entiy1
{
public int Id { get; set; }
public int DestinationId { get; set; }
public string Name { get; set; }
public string JobTitle { get; set; }
}
public class Entiy2
{
[Key]
public int DestinationId { get; set; }
public int DestinationName { get; set; }
}
public class EntityDTO
{
public int DestinationName { get; set; }
public int DestinationId { get; set; }
public string Name { get; set; }
public string JobTitle { get; set; }
}
Data is something like this : 数据是这样的:
Entity1 :
实体1:
Id=1 , DestinationId=1,Name=Name1,JobTitle=Job1Id = 1,DestinationId = 1,Name = Name1,JobTitle = Job1
Id=2 , DestinationId=1,Name=Name2,JobTitle=Job2Id = 2,DestinationId = 1,Name = Name2,JobTitle = Job2
Id=3 , DestinationId=2,Name=Name3,JobTitle=Job3Id = 3,DestinationId = 2,Name = Name3,JobTitle = Job3
Id=4 , DestinationId=2,Name=Name4,JobTitle=Job4Id = 4,DestinationId = 2,Name = Name4,JobTitle = Job4
Id=5 , DestinationId=2,Name=Name5,JobTitle=Job5Id = 5,DestinationId = 2,Name = Name5,JobTitle = Job5
Entity 2:实体2:
DestinationId=1 , DestinationName=Destination1DestinationId = 1,DestinationName = Destination1
DestinationId=2 , DestinationName=Destination2DestinationId = 2,DestinationName = Destination2
How can I select distinct destinationId and select Name and JobTitle from Entity1 then join them with Entity2 to fetch destination name and returning them as EntityDTO ? 如何选择不同的destinationId并从Entity1中选择Name和JobTitle,然后将它们与Entity2结合起来以获取目标名称并将其作为EntityDTO返回?
Here's a way to do it: 这是一种方法:
var query = from e1 in
(from e1 in entities1
group e1 by e1.DestinationId into grp
select grp.First())
join e2 in entities2 on e1.DestinationId equals e2.DestinationId
select new EntityDTO
{
DestinationId = e1.DestinationId,
DestinationName = e2.DestinationName,
Name = e1.Name,
JobTitle = e1.JobTitle
} ;
The trick is the group by
and then taking the first element of the grouping. 诀窍是
group by
,然后取分组的第一元素。 This is also referred to as "distinct by" that a library like MoreLinq provides out of the box. 这也称为“与众不同”,例如MoreLinq之类的库提供了开箱即用的功能。
Using LINQ extensions, I'm more of a fan of them: 使用LINQ扩展,我更喜欢它们:
var results = entityList1
.GroupBy(e => e.DestinationId)
.Select(e => e.First())
.Join(entityList2, e1 => e1.DestinationId, e2 => e2.DestinationId, (e1, e2) =>
new EntityDTO
{
DestinationId = e1.DestinationId,
DestinationName = e2.DestinationName,
JobTitle = e1.JobTitle,
Name = e1.Name
});
Same thing as Gert's anwser really. 确实和Gert的答案相同。 You can use
Distinct
but, you would have to inherit from IEquatible<T>
and implement the Equals
method and override the GetHashCode
method to get it to work. 您可以使用
Distinct
但是必须从IEquatible<T>
继承并实现Equals
方法,并重写GetHashCode
方法以使其起作用。
You can use the LINQ join operator like this: 您可以像这样使用LINQ join运算符:
var results = from e1 in context.Entity1s
join e2 in context.Entity2s
on e1.DestinationId equals e2.DestinationId
select new EntityDTO
{
DestinationId = e1.DestinationId,
Name = e1.Name,
JobTitle = e1.JobTitle,
DestinationName = e2.DestinationName
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.