c# - Linq Query and Expected Results -- Distinct Field -
i have nested linq
sql
query used populate treeview
. first select (title
field) when being bound treeview
showing of titles
correctly, if there multiple occurences want show once. how can solve this?
i tried
}).distinct().orderby(c => c.linenumber) }).distinct();
full query
var results = (from d in mcontext.docs orderby d.title ascending join b in mcontext.base on d.docid equals b.docid join h in mcontext.viewdocitems on b.baseid equals h.baseid h.itemid == mguid select new { id = d.docid, title = d.title, classid = d.classid, basehis = (from c in d.base select new basehistory() { baseid = c.baseid, name = c.title, basefinal = c.final.value, linenumber = c.linenumber.value itemid = h.itemid }).distinct().orderby(c => c.linenumber) }); foreach(var r in results) { var hist = new dochistory() { id = r.id, title = r.title, classid = r.classid; }; foreach(var h in r.basehis) { hist.basehis.add(h); } mhistory.add(hist); }
replace distinct()
by
.groupby(x => x.name).select(x => x.firstordefault())
that means grouping on name
(i.e. title
) , take first element of each group.
distinct not work, because, translated sql filters out records unique values each value of baseid
, title
, etc.
Comments
Post a Comment