c# - getting records from list based on category -
query:
list<vwbooktitlekitssummary> thesekits = (from k in _datacontext.vwbooktitlekitssummaries k.datekitends > datetime.now && k.datekitstarts <= datetime.now && k.isdeleted == false orderby k.kitorder, k.kitcode descending select k).tolist();
thesekits got 6 results below (the highlighted ones):
i want query returns 1 item each category example want records k1503, t1503 i.e.
i want first record start k , first record start t
i tried following:
list<vwbooktitlekitssummary> thesekits = (from k in _datacontext.vwbooktitlekitssummaries k.datekitends > datetime.now && k.datekitstarts <= datetime.now && k.isdeleted == false && (k.kitcode.startswith("k")) orderby k.kitorder, k.kitcode descending select k).take(1).tolist(); list<vwbooktitlekitssummary> qry = (from k in _datacontext.vwbooktitlekitssummaries k.datekitends > datetime.now && k.datekitstarts <= datetime.now && k.isdeleted == false && (k.kitcode.startswith("t")) orderby k.kitorder, k.kitcode descending select k).take(1).tolist(); thesekits.addrange(qry);
this return records kitcode - k1503, t1503
instead of writing 2 queries , using addrange can done in 1 query, please.
this should give 1 record per category, starting letters in category.
list<vwbooktitlekitssummary> firstcategorythesekits = _datacontext .vwbooktitlekitssummaries.where(k => k.datekitends > datetime.now && k.datekitstarts <= datetime.now && k.isdeleted == false).orderby(k => k.kitorder) .thenbydescending(k => k.kitcode).groupby(k => k.kitcode[0]).select(grps => grps.first) .tolist();
if want t or k, can add condition follows:
list<vwbooktitlekitssummary> firstcategorythesekits = _datacontext .vwbooktitlekitssummaries.where(k => k.datekitends > datetime.now && k.datekitstarts <= datetime.now && k.isdeleted == false && (k.kitcode[0] == 't' || k.kitcode[0] == 'k')).orderby(k => k.kitorder) .thenbydescending(k => k.kitcode).groupby(k => k.kitcode[0]) .select(grps => grps.first).tolist();
sorry query in concise notation, instead of query notation.
Comments
Post a Comment