我想了解linq如何工作。 我寫了一個測試應用程序,它不按我期望的方式工作。 從以下代碼,我期待看到項目「test1」和「test4」分組在一起,但我沒有得到。相反,我回來了4個獨立的團體。這意味着其中一個項目被分組在一起。 有人可以解釋我做錯了什麼? 謝謝。C#linq groupby返回不正確的組
public class linqtest
{ public int x1;
public int x2;
public string x3;
public linqtest(int a, int b, string c)
{
x1 = a;
x2 = b;
x3 = c;
}
public bool Equals(linqtest other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return x1 == other.x1 &&
x2 == other.x2;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof(linqtest)) return false;
return Equals((linqtest)obj);
}
}
linqtest tc14 = new linqtest(1, 4, "test1");
inqtest tc15 = new linqtest(3, 5, "test2");
linqtest tc16 = new linqtest(3, 6, "test3");
linqtest tc16a = new linqtest(1, 4, "test4");
List<linqtest> tclistitems = new List<linqtest>();
tclistitems.Add(tc14);
tclistitems.Add(tc15);
tclistitems.Add(tc16);
tclistitems.Add(tc16a);
IEnumerable<IGrouping<linqtest, linqtest>> tcgroup = tclistitems.GroupBy(c => c);
爲什麼tcgroup包含4組?我期待3組。
發帖前是否檢查過警告?我希望編譯器至少警告你,你不重寫GetHashCode。 – 2013-04-25 15:17:06
謝謝。不,我沒有得到一個警告,我應該重寫GetHashCode。 – user2070073 2013-04-25 15:38:32
你用什麼來編譯?我剛剛編譯了*精確*代碼並收到:「Test.cs(7,14):warning CS0659:'linqtest'覆蓋Object.Equals(object o),但不覆蓋Object.GetHashCode()。我會試着在其他方面解決這個問題 - 重要的是你可以看到警告。 – 2013-04-25 15:51:22