I am trying to count the elements in a group. So I have this LINQ to Entities query:
var qry2 = from c in qry group c by c.Content.DownloadType into grouped select new KeyValuePair(grouped.Key,grouped.Count());
But this does not work, because LINQ to Entities accepts only parameter initializers or constructors without parameters. So I created a simple class to omit the KeyValuePair type:
public class ValueCount { public string Key { get; set; } public int Value { get; set; } public KeyValuePair<string, int> ToKeyValuePair() { return new KeyValuePair<string, int>(this.Key, this.Value); } }
And changed the request to:
var qry2 = from c in qry group c by c.Content.DownloadType into grouped select new ValueCount { Key = grouped.Key, Value = grouped.Count() }.ToKeyValuePair();
But still does not work. It says that it does not recognize the ToKeyValuePair () method
How can I compile KeyValuePairs from a LINQ to Entities query?
source share