简体   繁体   English

实体框架中的不同记录

[英]Distinct Records in Entity framework

I want to Distinct List of Data 我想区分数据列表

My Code: 我的代码:

PendingGRemsResult _Return = new PendingGRemsResult();

_Return.PendingGRemsResultRecord = Context.Where(a => a.Status == 0)
   .Select(s => new PendingGRemsResultRecord
     { GUID = s.GRemGUID.GUID,
       Count = App.GroupedRemittance
         .GetByFilter((gr => gr.GRemGUID.GUID == s.GRemGUID.GUID))
         .Count(),
       CreatorType = s.GRemGUID.CreatorType.Value})
  .Distinct()
  .ToList();

return _Return;

this code not working data sill duplicated 此代码无法正常工作的数据表重复

There are at least 3 solutions to this challenge: 至少有3种解决方案来应对这一挑战:

  1. Use DistinctBy() method with lambda expression, example: 对Lambda表达式使用DistinctBy()方法,例如:

    var query = someData.DistinctBy(x => x.PropertyYouWantToDistinquishBy) ; var query = someData.DistinctBy(x => x.PropertyYouWantToDistinquishBy) ;

  2. Build an equalityComparer that will implement the interface IEqulityComparer<PendingGRemsResultRecord> and then use an instance of this class for the overload od Distinct() method. 构建将实现接口IEqulityComparer<PendingGRemsResultRecord> ,然后将此类的实例用于重载od Distinct()方法。

  3. Implement the IEquatable interface in your PendingGRemsResultRecord class (with Equals and GetHashCode methods). PendingGRemsResultRecord类中实现IEquatable接口(使用EqualsGetHashCode方法)。

_Return.PendingGRemsResultRecord = 
     Context.Where(a => a.Status == 0)
            .Select(a => new {
               a.GRemGUID.GUID, 
               CreatorType = a.s.GRemGUID.CreatorType.Value })
            .Distinct()
            .Select(x => new PendingGRemsResultRecord {
                  GUID = x.GUID,
                  Count = App.GroupedRemittance
                             .GetByFilter(gr => gr.GRemGUID.GUID == x.GUID)
                             .Count(),
                  CreatorType = x.CreatorType
              }).ToList();

UPDATE: How it works - thus you don't have Equals and GetHashCode specified on you PendingGRemsResultRecord class, then instances of this class will be compared by reference. 更新:它是如何工作的-因此,您没有在PendingGRemsResultRecord类上指定EqualsGetHashCode ,则将通过引用比较该类的实例。 And that will treat all instances to be different, even if they have same values of properties. 即使所有实例具有相同的属性值,这也将使其视为不同。 You have several possibilities to teach Distinct() method to look on values instead of comparing references (override Equals and GetHasCode , create and pass custom comparer) but you also can use power of anonymous objects, which have default Equals and GetHashCode implementations that use values of properties to check if two anonymous objects are equal. 您可以教导Distinct()方法查看值而不是比较引用(重写EqualsGetHasCode ,创建并传递自定义比较器),但您还可以使用匿名对象的功能,这些对象具有使用值的默认EqualsGetHashCode实现属性来检查两个匿名对象是否相等。

That's exactly what I use here - projecting source sequence objects into anonymous objects with GUID and CreatorType properties. 这正是我在这里使用的-将源序列对象投影到具有GUIDCreatorType属性的匿名对象中。 Then with power of default comparison of anonymous objects you can use Distinct() to get only objects with different guid and creator type. 然后,借助默认比较匿名对象的功能,您可以使用Distinct()仅获取具有不同GUID和创建者类型的对象。 Next is simple projecting of distinct results to your PendingGRemsResultRecord . 接下来是将不同结果简单投影到您的PendingGRemsResultRecord

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

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