So you have a sequence of ZipSearchHistories
. Every ZipSearchHistory in this sequence has at least a ZipCode, a State and a City.
Some of these ZipSearchHistories have the same values for {ZipCode, State, City}. You want to count for every combination of {ZipCode, State, City} the number of ZipSearchHistories that have this combination.
You are right, for this you'll need one of the overloads of Queryable.GroupBy. I most often use the overload that has a parameter resultSelector. With this parameter you can specify the output per group.
var result = dbContext.ZipSearchHistories(
// parameter keySelector: make groups with same values for {ZipCode, State, City}
zipSearchHistory => new
{
ZipCode = zipSearchHistory.ZipCode,
State = zipSearchHistory.State,
City = zipSearchHistory.City,
},
// parameter resultSelector: for every key {ZipCode, State, City},
// and all ZipSearchHistories that have this {ZipCode, State, City} make one new:
(key, zipSearchHistoriesWithThisKey) => new
{
ZipCode = key.ZipCode,
State = key.State,
City = key.City,
Count = zipSearchHistoriesWithThisKey.Count(),
})
In words: make groups of ZipSearchHistories, that have the same value for {ZipCode, State, City}. From every group (where all elements have this {ZipCode, State, City}) make one new object, that has these {ZipCode, State, City} and the number of elements in the group.