vb.net - Converting VB Linq to C# -
i'm in process of teaching myself c# converting existing project , stuck converting following vb linq code:
dim outstuff = tt in (from t in products.selectmany(function(p) if(p.tags isnot nothing, p.tags, new observablecollection(of tagmodel))) group tagname = t.name, v = (aggregate p in products if(p.tags isnot nothing, p.tags.contains(t), nothing) sum(p.views)), nl = (aggregate p in products if(p.tags isnot nothing, p.tags.contains(t), nothing) sum(p.num_likes)) g = group, count()) group name = tt.tagname count = sum(tt.count), viewstotal = sum(tt.v), num_likestotal = sum(tt.nl) select name, count, viewstotal, num_likestotal where products observablecollection(of productmodel)
i've mananged convert far:
var x = products.selectmany(p => (p.tags != null) ? p.tags : new observablecollection<tagmodel>()); var tags = t in x group t t.name g select new { tagname=g.first().name}; the 'group by's has me stumped. great...
your query little convoluted , hard follow, let me try describe think looking for. have list of products, each of may have 1 or more tags; , want list of of tags, count of how many products have tag, total number of views of products tag, , total number of "likes" of product tag. if case, following should trick:
// may want add toarray() here filter not executed multiple times during subsequent query var productswithtags = products.where(p => p.tags != null); var outstuff = t in (from p in productswithtags t in p.tags select t).distinct() let matchingproducts = productswithtags.where(p => p.tags.contains(t)) select new { name = t.name, count = matchingproducts.count(), viewstotal = matchingproducts.sum(p => p.views), num_likestotal = matchingproducts.sum(p => p.num_likes) };
Comments
Post a Comment