2017-09-25 1 views
1

C# WPF에서 응용 프로그램을 개발 중이며 데이터 용 EF6.0 프레임 워크를 사용하고 있습니다. 관찰 가능한 컬렉션을 필터링하거나 특정 속성의 고유 한 값을 표시하는 가장 좋은 방법은 무엇인지 혼란스럽지 않습니다. 이 코드를 사용하여 시도했지만 성공하지 못했습니다. 이것은 고유 SW 버전 public void getuniquesw()을 필터링하려고 시도하는 방법입니다. Iequality 비교 방법을 확인했지만 이해할 수 없었습니다. 필터링/고유 값을 수행하는 가장 쉬운 방법은 무엇이 될 것입니다. 엔티티 클래스의C# WPF 필터 관찰 가능 컬렉션

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()); 
} 

정의

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; } 

    } 
} 
+0

아무도 당신을 도울 수있는 여기에 하나의 분명한 문제가 있음을 알아 두십시오. 솔직히, 당신은 몇 가지 개념을 근본적으로 오해 한 것처럼 보입니다. 'ObservableCollection'이 사용하는 방식에 따라 해결해야하는 문제를 이해한다는 것은 확실하지 않습니다. 나는 당신이'getuniquesw'의 구현에 따라 유형을 완전히 이해하는지 확신하지 못합니다. 그리고 'Iequality comparable method'가 어떻게이 문제에 부합하는지 잘 모르겠습니다. 나는 한 걸음 물러나서 각 이슈를 한 번에 하나씩 다루는 방법을 찾아야한다고 생각합니다. 죄송합니다. 더 도움을 드릴 수 없습니다. –

+0

@JasonBoyd 감사합니다. 유형 기반 구현에 대해 더 자세히 살펴 보겠습니다. –

+0

@ mm8 정의를 추가했습니다 –

답변

1

IEqualityComparer<CREntity> 구현하는 클래스 만들기 :

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(); 
    } 
} 

을 ... 그리고 Distinct() 방법이 하나의 인스턴스를 전달합니다

public void getuniquesw() 
{ 
    CRmappings2 = new ObservableCollection<CREntity>(crentities.Select(p => p.SW_Version).Distinct(new CREntityComparer())); 
} 
+0

도움을 주셔서 감사합니다. 그래서 observable 컬렉션을 사용한다면 항상 Iequalitycomparer를 사용해야합니다. 다른 필터 방법도 있습니까? –

+0

거의 모든 것을 다룰 수있는 방법은 다양합니다. 그러나이 경우 두 개의 CREntity 객체가 같은 것으로 간주 될 때를 정의해야합니다. – mm8

+0

네, 이제 내 명성에 감동 15 이제 나는 upvote 수 있습니다 :) –

관련 문제