How can i convert this to linq?
select i.CatName,COUNT(*) as n_items
from ixcxs i
group by i.CatName
or lambda expression will be helpful
You don't need to select first item from group to get CatName - use grouping key instead:
var query = from i in ixcxs
group i by i.CatName into g
select new {
CatName = g.Key,
n_items = g.Count()
};
Same with methods syntax:
var query = ixcxs.GroupBy(i => i.CatName)
.Select(g => new { CatName = g.Key, n_items = g.Count() });
Try
ixcxs.GroupBy(g => g.CatName)
.Select(s => new
{
CatName = s.Key,
n_items = s.Count()
});
same thing in query syntax:
from i in ixcxs
group i by i.CatName into g
select new
{
CatName = g.Key,
n_items = g.Count()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With