c# - What's the better way to write this linq query? -
sql:
select node.categoryid, node.categoryname, node.description, node.lft, node.rgt, node.showonmenu, (count(parent.categoryname) - 1) level, (case when node.lft = node.rgt - 1 'true' else 'false' end) leaf article_category node, article_category parent node.lft between parent.lft , parent.rgt group node.categoryid,node.categoryname,node.description,node.lft,node.rgt,node.showonmenu order node.lft
my linq expression:
var list = (from node in dbcontext.categories parent in dbcontext.categories node.lft >= parent.lft && node.lft <= parent.rgt select new { node.categoryid, node.categoryname, node.description, node.lft, node.rgt, node.showonmenu, parentname = parent.categoryname, } x group x new { x.categoryid, x.categoryname, x.description, x.lft, x.rgt, x.showonmenu, } g orderby g.key.lft select new { categoryid = g.key.categoryid, categoryname = g.key.categoryname, description = g.key.description, lft = g.key.lft, rgt = g.key.rgt, showonmenu = g.key.showonmenu, level = g.count() - 1, isleaf = g.key.lft == g.key.rgt - 1 }).tolist();
my question:
the linq expression long, there 2 'select new' expressions, wonder how make shorter?
what's corresponding extension method linq query? how can express "from... from...where..." extension method?
the first select new .. x
don't see why need, try removing , write group node new...
"from...from"
written lambda expression this:
categories.selectmany(n => categories, (n, p) => new { node = n, parent = p });
Comments
Post a Comment