C#에서 Linq 사용 시 Collection에 대해 중복제거를 하고 싶다면 IEqualityComparer를 상속받은 클래스를 구현하여 Distinct() 사용 시 인자값으로 IEqualityComparer 객체를 넣어주게 됩니다.
이 때 조건을 특정 조건을 더 넣어서 제거되게 되는 대상을 특정할 수 있을까요?
using System.Diagnostics.CodeAnalysis;
class CustomItem
{
public int Idx { get; set; }
public int intValue { get; set; }
public string strValue { get; set; }
public long longValue { get; set;}
}
class CustomItemIntEqualityComparer : IEqualityComparer<CustomItem>
{
public bool Equals(CustomItem x, CustomItem y)
{
if (x.intValue == y.intValue)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode([DisallowNull] CustomItem obj)
{
int hash = obj.intValue.GetHashCode();
return hash;
}
}
class CustomItemStringEqualityComparer : IEqualityComparer<CustomItem>
{
public bool Equals(CustomItem x, CustomItem y)
{
if (x.strValue.Equals(y.strValue))
{
return true;
}
else
{
return false;
}
}
public int GetHashCode([DisallowNull] CustomItem obj)
{
int hash = obj.strValue.GetHashCode();
return hash;
}
}
class CustomItemLongEqualityComparer : IEqualityComparer<CustomItem>
{
public bool Equals(CustomItem x, CustomItem y)
{
if (x.longValue == y.longValue)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode([DisallowNull] CustomItem obj)
{
int hash = obj.longValue.GetHashCode();
return hash;
}
}
List<CustomItem> list = new()
{
new CustomItem
{
Idx = 0,
intValue = 1,
strValue = "일루수가 누구야",
longValue = 100,
},
new CustomItem
{
Idx = 1,
intValue = 1,
strValue = "이루수가 누구야",
longValue = 200,
},
new CustomItem
{
Idx = 2,
intValue = 1,
strValue = "일루수가 누군데",
longValue = 200,
},
new CustomItem
{
Idx = 3,
intValue = 1,
strValue = "이루수가 누구야",
longValue = 400,
},
};
Console.WriteLine("[INT]");
List<CustomItem> intDistinctList = list.Distinct(new CustomItemIntEqualityComparer()).ToList();
foreach (CustomItem item in intDistinctList)
{
Console.WriteLine($"Idx: {item.Idx}, int: {item.intValue}, string: {item.strValue}, long: {item.longValue}");
}
Console.WriteLine("--------------------------------------------------------------------------------------------------");
Console.WriteLine("[STRING]");
List<CustomItem> strDistinctList = list.Distinct(new CustomItemStringEqualityComparer()).ToList();
foreach (CustomItem item in strDistinctList)
{
Console.WriteLine($"Idx: {item.Idx}, int: {item.intValue}, string: {item.strValue}, long: {item.longValue}");
}
Console.WriteLine("--------------------------------------------------------------------------------------------------");
Console.WriteLine("[LONG]");
List<CustomItem> longDistinctList = list.Distinct(new CustomItemLongEqualityComparer()).ToList();
foreach (CustomItem item in longDistinctList)
{
Console.WriteLine($"Idx: {item.Idx}, int: {item.intValue}, string: {item.strValue}, long: {item.longValue}");
}
Console.WriteLine("--------------------------------------------------------------------------------------------------");
이 코드를 실행하면 매번 같은 결과가 출력됩니다.
여기서 첫번째 int값으로 중복제거를 할 때 Idx가 0번인 값 이외의 값이 출력되도록 할 수 있나요?
소스는 vscode에서 .NET Interactive에서 실행하시거나, .NET 5 Console APP에서 실행하시면 됩니다.