简体   繁体   English

Entity Framework Core 将 DISTINCT 发送到生成的 SQL

[英]Entity Framework Core send DISTINCT into the generated SQL

Is it possible to do send the DISTINCT keyword into the generated SQL command using Entity Framework Core?是否可以使用 Entity Framework Core 将DISTINCT关键字发送到生成的 SQL 命令中? All the examples I see on grabbing distinct values is calling the .Distinct() after the values are already retuned, so the database is sending back a lot of data, when the database could do the distinct itself.我在获取不同值时看到的所有示例都是在值已经重新调整后调用.Distinct() ,因此数据库正在发送回大量数据,而数据库本身可以执行不同的操作。

This would be for an anonymous object, with lots of values inside of it, from ints to strings and byte arrays.这将用于匿名 object,其中包含许多值,从整数到字符串和字节 arrays。

I know I could do the .GroupBy().Select() , but again that is after the data has arrived, not before.我知道我可以做.GroupBy().Select() ,但同样是在数据到达之后,而不是之前。

Example doesn't work with anonymous types:示例不适用于匿名类型:

(From a In DatabaseContext.Associate
 Join ac In DatabaseContext.AssociateClassification On ac.AssociateId Equals a.AssociateId
 Join ad In DatabaseContext.AssociateDepartment On ad.AssociateId Equals a.AssociateId
 Join ass In DatabaseContext.AssociateScheduleStaffing On ass.AssociateId Equals a.AssociateId
 Group Join ap In DatabaseContext.AssociatePhoto On ap.AssociateId Equals a.AssociateId Into apg = Group
 From ap In apg.DefaultIfEmpty()
 Where ac.ClassificationId.Value = ClassificationId AndAlso
       a.Inactive.Value = Convert.ToByte(0) AndAlso
       departments.Contains(ad.Department) AndAlso
       ass.UseJbDev.Value = Convert.ToByte(1) AndAlso
       Not DatabaseContext.Schedule.Any(Function(s) s.AssociateId = a.AssociateId AndAlso s.StartTime.Value >= Day AndAlso s.StartTime.Value < Day.AddDays(1))
 Select a.AssociateId, a.AssociateNumber, a.FullName, a.Inactive, ap.Photo, ass.DateSeniority).Distinct().ToList()

FYI: It does work however if I remove the ap.Photo and ass.DateSeniority and all objects then are the 'Associate' type, and Distinct works.仅供参考:但是,如果我删除 ap.Photo 和 ass.DateSeniority 并且所有对象都是“关联”类型,那么它确实有效,并且 Distinct 有效。

But again, it is after the fact, because when looking at the SQL created, it is not a SELECT DISTINCT, nor does the DISTINCT appear in the SQL.但同样,这是事后的,因为在查看创建的 SQL 时,它不是 SELECT DISTINCT,DISTINCT 也没有出现在 Z9778840A0100CB30C982876741B0B0 中。

Seems to me the answer is that "Distinct" extension method doesn't compare values of complex type objects.在我看来,答案是“不同”扩展方法不比较复杂类型对象的值。 I need to implement IEqualityComparer<T> interface in order to compare the values of complex types.我需要实现IEqualityComparer<T>接口以比较复杂类型的值。

That is the reason it worked fine if all properties of the object are from the same type, when you mix the properties and create an anonymous type, the built in compare doesn't work and need to implement your own comparer.这就是如果 object 的所有属性都来自同一类型时它工作正常的原因,当您混合属性并创建匿名类型时,内置比较不起作用并且需要实现您自己的比较器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM