简体   繁体   English

可以在Distinct属性上枚举过滤实体框架

[英]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. 确实,我应该只能告诉该对象是唯一的,因为this._parentAssemblyBOM.partNumber将是distinct属性。 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());

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

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