Reputation: 239
So Ive been trying to get this linq query to properly query but it is just counting by the records and not repeated entries.
var groupedList = from x in WarningList
group WarningList by new {etlTraceMessage.Message}
into grouped
select new {grouped.Key.Message, Count = grouped.Count()};
Im trying to get a result set like...
15 - Missing ID
4 - Invalid use of term
1 - Package missing
But its only querying as a count of records and not how many per each type etc.
20 - Missing ID
Warninglist is just an observablecollection class with a few properties
Thanks
Upvotes: 1
Views: 733
Reputation: 10376
I am not sure what is your classes for WarningList
, but let me assume that it has a Message
property and you want to group your list by this property:
public class Warn
{
public string Message {get; set;}
//other properties
}
If so - you can easily group your list like this:
var groupedList = WarningList
.GroupBy(x => x.Message)
.Select(g => new {Key = g.Key, Count = g.Count()});
Or if you want another sintax:
var groupedList = from x in WarningList
group x by x.Message into grouped
select new { Key = grouped.Key, Count = grouped.Count()};
In your approach you tried to group by a composite key (which is unnecessary) but used some different local variable - not linked to your query (etlTraceMessage.Message
). But if you will need it later:
var groupedList = from x in WarningList
group x by new { x.Message } into grouped
select new { Key = grouped.Key.Message, Count = grouped.Count()};
And just in case - msdn has a lot of examples.
Upvotes: 3