Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF中的过滤器可观测集合_C#_Wpf_Entity Framework_Data Binding - Fatal编程技术网

C#WPF中的过滤器可观测集合

C#WPF中的过滤器可观测集合,c#,wpf,entity-framework,data-binding,C#,Wpf,Entity Framework,Data Binding,我正在用C#WPF开发一个应用程序,并使用EF6.0数据框架。我不太清楚过滤可观察集合或显示特定属性的不同值的最佳方法是什么。我试着用这段代码来做,但没有成功。此方法尝试筛选唯一的软件版本public void getuniquesw()。我检查了这个方法,但不明白。过滤/区分值的最简单方法是什么 public List<CREntity> crentities { get; set; } // Obeservable collection property for

我正在用C#WPF开发一个应用程序,并使用EF6.0数据框架。我不太清楚过滤可观察集合或显示特定属性的不同值的最佳方法是什么。我试着用这段代码来做,但没有成功。此方法尝试筛选唯一的软件版本
public void getuniquesw()
。我检查了这个方法,但不明白。过滤/区分值的最简单方法是什么

public List<CREntity> crentities
{
    get;
    set;
}

// Obeservable collection property for access
private ObservableCollection<CREntity> _CRmappings2 = new ObservableCollection<CREntity>();
public ObservableCollection<CREntity> CRmappings2
{
    get { return _CRmappings2; }
    set
    {
        _CRmappings2 = value;
        RaisePropertyChanged("CRmappings2");
    }
}

public void UpdatePopList()
{
    CRmappings2 = new ObservableCollection<CREntity>(crentities.Where(p => p.MU_Identifier == selectmu.ToString()).ToList()); 
}

public void getuniquesw()
{

    CRmappings2 = new ObservableCollection<CREntity>(crentities.Select(p=>p.SW_Version).Distinct());
}
公开列表凭证
{
得到;
设置
}
//用于访问的Obeservable集合属性
私有ObservableCollection_CRmappings2=新ObservableCollection();
公共可观测收集CRmappings2
{
获取{return\u CRmappings2;}
设置
{
_CRmappings2=数值;
RaisePropertyChanged(“CRmappings2”);
}
}
public void updateplist()
{
CRmappings2=新的ObservableCollection(crenties.Where(p=>p.MU_Identifier==selectmu.ToString()).ToList());
}
public void getuniquesw()
{
CRmappings2=新的ObservableCollection(crenties.Select(p=>p.SW_Version.Distinct());
}
实体类的定义

using DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusinessEntities
{
    public class CREntity
    {
        public long C_ID { get; set; }
        public string LogName { get; set; }
        public string xSCR_BUG { get; set; }
        public string RequestType { get; set; }
        public string MU_Type { get; set; }
        public long CPC2_OBD_1Byte { get; set; }
        public long INS_OBD_1Byte { get; set; }
        public string MU_Identifier { get; set; }
        public string Old_MU { get; set; }
        public int? SPN { get; set; }
        public int? FMI { get; set; }
        public string Triggers { get; set; }
        public string Comment { get; set; }
        public string Status { get; set; }
        public string SW_Version { get; set; }
        public DateTime? Create_Date { get; set; }
        public DateTime? Upd_Date { get; set; }
        public virtual ICollection<Fault_Details> FaultDetails { get; set; }

    }
}
使用数据模型;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间业务实体
{
公开课信度
{
公共长C_ID{get;set;}
公共字符串日志名{get;set;}
公共字符串xSCR_BUG{get;set;}
公共字符串请求类型{get;set;}
公共字符串MU_类型{get;set;}
公共长CPC2_obd1byte{get;set;}
公共长字节{get;set;}
公共字符串MU_标识符{get;set;}
公共字符串Old_MU{get;set;}
公共int?SPN{get;set;}
公共int?FMI{get;set;}
公共字符串触发器{get;set;}
公共字符串注释{get;set;}
公共字符串状态{get;set;}
公共字符串SW_版本{get;set;}
公共日期时间?创建_日期{get;set;}
公共日期时间?Upd_日期{get;set;}
公共虚拟ICollection FaultDetails{get;set;}
}
}

创建一个实现
IEqualityComparer
的类:


我不知道这里有一个明确的问题,任何人都可以帮助你。老实说,你似乎对一些概念有着根本性的误解。您是否理解
observateCollection
要根据您使用它的方式来解决的问题还不清楚。我不确定您是否完全理解基于
getuniquesw
实现的类型。我不确定你们怎么认为“质量可比方法”适合这个。我认为你需要退后一步,想办法一次解决一个问题。很抱歉,我不能提供更多帮助。@JasonBoyd谢谢,我将进一步检查基于类型的实现。@mm8我添加了定义谢谢你的帮助,这非常有效。所以我应该总是使用Iequalitycomparer如果我们使用的是observable集合,还有其他不同的过滤方法吗?几乎所有的事情都有不同的方法。但在这种情况下,您需要定义两个CREntity对象何时被视为相等。是的,现在我的声誉达到15,现在我可以投票:)
public class CREntityComparer : IEqualityComparer<CREntity>
{
    public bool Equals(CREntity x, CREntity y)
    {
        if (x != null && y != null && x.C_ID.Equals(y.C_ID))
            return true;

        return false;
    }

    public int GetHashCode(CREntity obj)
    {
        if (obj == null)
            return -1;

        return obj.C_ID.GetHashCode();
    }
}
public void getuniquesw()
{
    CRmappings2 = new ObservableCollection<CREntity>(crentities.Select(p => p.SW_Version).Distinct(new CREntityComparer()));
}