c# - RavenDB count index including zero values -
i have list of items {id, name, categoryid}
, list of categories {id, name, isactive}
.
how list {categoryid, count}
including categories have 0 items.
currently have such index:
public class categorycountindex : abstractindexcreationtask<item, categorycountindex.result> { public class result { public string categoryid { get; set; } public int count { get; set; } } public categorycountindex() { map = items => item in items select new result { categoryid = item.categoryid, count = 1 }; reduce = results => result in results group result result.categoryid c select new result { categoryid = c.key, count = c.sum(x => x.count) }; } }
what best way improve/change solution in order have categories no items?
i removed earlier answer proved incorrect. in case can use multimap/reduce index solve problem.
try using following index:
public class category_items : abstractmultimapindexcreationtask<category_items.reduceresult> { public class reduceresult { public string categoryid { get; set; } public int count { get; set; } } public category_items() { addmap<item>(items => item in items select new { categoryid = item.categoryid, count = 1 }); addmap<category>(categories => category in categories select new { categoryid = category.id, count = 0 }); reduce = results => result in results group result result.categoryid g select new reduceresult { categoryid = g.key, count = g.sum(x => x.count) }; } }
this result in following (three categories, 1 without items):
now can use result transformer if want display category name.
hope helps!
Comments
Post a Comment