简体   繁体   中英

Filter Entity Framework enumerable on Distinct property

The following statement is not returning distinct values, but the whole list:

    public ObservableCollection<MasterPartsList> ParentAssemblyBOM
    {
        get
        {
            var enumerable = this._parentAssemblyBOM
                                    .Where(parent => parent.isAssy == true).Distinct();
            return new ObservableCollection<MasterPartsList>(enumerable) ;

        }

Truly, I should only be able to tell that the object is unique because this._parentAssemblyBOM.partNumber would be the distinct property. How do I work in this logic to yield the correct results?

Thanks in advance!

Try grouping by the identifier (in your case part number) and then select the first of the group:

 var enumerable = this._parentAssemblyBOM
                                .Where(parent => parent.isAssy == true)
                                .GroupBy(x => x.partNumber)
                                .Select(x => x.FirstOrDefault());

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