flags
처럼 보일 것이고, 예상대로 별개 작동하지 않습니다! Distinct()는 목록의 플래그 값이 아니라 해당 메모리 참조 (모두 다름)에서 작동하기 때문입니다.
등호를 비교하는 방법을 가르치는 비교 자 클래스를 작성해야합니다.
class FlagComparer : IEqualityComparer<flag>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(flag x, flag y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the products' properties are equal.
return x.Code == y.Code && x.Name == y.Name;
}
}
을하고,이 명세서 전화 :
public class flag
{
public string Name { get; set; }
public string Code { get; set; }
}
는이 같은 비교 자 클래스를 생성해야합니다 알고, 뚜렷한 방법이 방법으로
List distinctFlags = flags.Distinct(new FlagComparer()).ToList();
이 플래그 클래스가 있다고 가정 정확히 일치하는 플래그를 istance 방법.
물론
class FlagComparer : IEqualityComparer<flag>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(flag x, flag y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the products' properties are equal.
return x.HostID == y.HostID && x.RuleID == y.RuleID && x.Flag == y.Flag && x.FlagValue == y.FlagValue;
}
}
이 모든 속성 값 형식이어야합니다 : 당신이 내 제안을 따르도록 wanto 경우
UPDATE 귀하의 의견을 바탕으로
, 당신은 다음과 같은 비교 자 기반을 작성해야 .
자신을 명확히하기 위해 여기를보세요 : 무엇
사용자의 입력 값은 당신은 출력으로 무엇을 기대 하는가? –
dbcontext 내부 또는 외부입니까? 제발 더 많은 코드를 보여주십시오. 가능한 경우 변환 전후에 데이터 예제를 제공하십시오. –
그룹을 만들어 승자를 선택하십시오. http://stackoverflow.com/questions/489258/linq-distinct-on-a-particular-property/491832#491832 –