LINQ 학습중 이해가 안되는 부분이 있어 질문올립니다.
- 무엇을 하고자 하는지.
-. 입력받은 컬랙션들 중에서 가장 중복이 많은 컬렉션 반환
-. 중복이 동일하다면, 가장 적은 컬랙션 반환
-. 빈 컬랙션일 경우 null 반환
*{1,2,3,4} - 중복 0 개
*{1,2,3,3,4,4,4} - 중복 3개 (3 - 1개, 4 - 2개)
*{1,2,3,3,4,4,4,5,6,7} - 중복 3개 (3 - 1개, 4 - 2개)
결과
: 두번째 컬랙션 {1,2,3,3,4,4,4}
- 현재 작성한 코드 중 문제가 되는 부분
private static void Print(string title, List<List<int>> result)
{
Console.WriteLine($"============= {title}");
for (int i = 0; i < result.Count(); i++)
{
Console.WriteLine(
string.Join(",",
Array.ConvertAll(result.ElementAt(i)
.ToArray(), i => i.ToString()))
);
}
}
private static void Test()
{
var collections = new List<List<int>> {
new List<int> { 1, 2, 3, 4 }, // 4 - 4 = 0
new List<int> { 1, 2, 3, 3, 4, 4, 4 }, // 7 - 4 = 3
new List<int> { 1, 2, 3, 3, 4, 4, 4, 5, 6, 7 } // 10 - 7 = 3
};
//var result = collections
// .OrderBy(list => list.Count() - list.Distinct().Count())
// .ThenByDescending(list => list.Count())
// .ToList()
//;//.LastOrDefault();
//Print("Result ***", result);
var result1 = collections
.OrderBy(list => list.Count() - list.Distinct().Count())
.ToList()
;//.LastOrDefault();
Print("OrderBy ***", result1);
var result2 = collections
.OrderBy(list => list.Count() - list.Distinct().Count())
.ThenByDescending(list => list.Count())
.ToList()
;//.LastOrDefault();
Print("ThenByDescending ***", result2);
}
============= OrderBy ***
1,2,3,4
1,2,3,3,4,4,4
1,2,3,3,4,4,4,5,6,7
============= ThenByDescending ***
1,2,3,4
1,2,3,3,4,4,4,5,6,7
1,2,3,3,4,4,4
여기서 ThenByDescending 의 결과가 왜 이렇게 나오는지 이해가 안됩니다;;;;
단순히 Count() 내림차순 정렬이면 10, 7, 4 순으로 정렬되야 하는게 맞지 않나요???
- 기대하는 동작
list.Count() 내림차순 10, 7, 4 인데, 4,10,7이 되는 이유를 알고 싶습니다.