简体   繁体   中英

Distinct doesn't work on Entity Framework

The .Distinct() does not work, as you can see it is returning duplicated records (with the same Id)

I also tried:

Vendors = c.Item.VendorSpecifications
           .Select(v => new APVendorSimpleView { Id = v.VendorId, Name = v.Vendor.Name, Code = v.Vendor.Code })
           .GroupBy(x => x.Id)
           .Select(x => x.First())
           .ToList(),

and it threw an exception

IMG1IMG2

Before explaining the Distinct. Your exception feels like a null ref exception coming from v.Vendor.Name or similar inside your select APVendorSimpleView . If Vendor is null you cannot access the Name property and you get an exception.

For the Distinct() here is what the Docs mention.

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8

If you want to return distinct elements from sequences of objects of some custom data type, you have to implement the IEquatable generic interface in the class. The following code example shows how to implement this interface in a custom data type and provide GetHashCode and Equals methods.

Does your APVendorSimpleView model meet the above criteria?

public class Product : IEquatable<Product>
{
    public string Name { get; set; }
    public int Code { get; set; }

    public bool Equals(Product other)
    {

        //Check whether the compared object is null. 
        if (Object.ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data. 
        if (Object.ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal. 
        return Code.Equals(other.Code) && Name.Equals(other.Name);
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public override int GetHashCode()
    {

         //Get hash code for the Name field if it is not null. 
         int hashProductName = Name == null ? 0 : Name.GetHashCode();

         //Get hash code for the Code field. 
         int hashProductCode = Code.GetHashCode();

         //Calculate the hash code for the product. 
         return hashProductName ^ hashProductCode;
     }
}

//Code example
Product[] products = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 }, 
                       new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "lemon", Code = 12 } };

//Exclude duplicates.

IEnumerable<Product> noduplicates =
    products.Distinct();

foreach (var product in noduplicates)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:
    apple 9 
    orange 4
    lemon 12
*/

How about

....
Vendors = c.Item?.VendorSpecifications?.GroupBy(x => new 
                                               { 
                                                   Id = x.VendorId, 
                                                   Name = x.Vendor.Name, 
                                                   Code = x.Vendor.Code 
                                               })
                                       .Select(x => new APVendorSimpleView 
                                               { 
                                                   Id = x.Key.Id, 
                                                   Name = x.Key.Name, 
                                                   Code = x.Key.Code 
                                               }), 
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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