我想將一個grouppedList傳遞給幾個函數,但是當嘗試使用一個類而不是一個匿名類型進行分組時,我得到了奇怪的結果。在以下示例中,匿名類型按預期返回2個結果,但具體類返回5,就好像它沒有分組一樣。Linq to Sql Group按類
問題:是否有可能通過具體的類來執行Linq to Sql Group?
public class person
{
public string Name;
public string State;
}
public class personGroup
{
public string State;
public personGroup(string personState)
{
State = personState;
}
}
void Main()
{
var people = new List<person>();
people.Add(new person {Name = "Bob", State = "Tx"});
people.Add(new person {Name = "Bill", State = "Tx"});
people.Add(new person {Name = "Tracy", State = "Tx"});
people.Add(new person {Name = "Steve", State = "Md"});
people.Add(new person {Name = "Kelly", State = "Md"});
var groupedPeople = people.GroupBy (p => p.State);
groupedPeople.Count().Dump();//Returns 2
var morePeople = people.GroupBy (p => new personGroup(p.State));
morePeople.Count().Dump();//Returns 5
}
在這個問題中沒有「SQL」。我想你只是說LINQ。 – 2014-11-06 21:26:43