c# - Optional Random ordered nested grouping in linq -
i in position can't think of answer regarding optional grouping in linq.
basically,i want generate report comes screen having filters. these filters(mostly grouped) optional , can rearranged.it's like
filters: 1.clients projects tasks duration or 2.projects clients tasks duration or 3.task duration etc.
with possible combinations. data should like
1.clienta projecta taska 26hrs 45mins taskb 43hrs 23mins projectb taskx...... 2.projecta clienta taska 26hrs 45mins... 3.taska 26hrs 45mins taskb 6hrs 35mins
i have data.but unable write logic generalized.
i thinking enum hold filters (viewmodel)selected enum.client,enum.project... and
if (clientgroupchecked) foreach(var clientgroup in list){ //group list client here if(projectgroupchecked) foreach(var projectgroup in clientgroup){ //group list project here } }
i know,it's wrong.this way have put logic combinations possible.
couldn't think of else.i want generalized because may have more filters added in future , don't want change entire logic filters(of course,i want add new filter group somewhere in logic.but want more easy maintain also.
edited:
@sschimmel :my point grouping can shuffled(for have buttons[selected -->green , unselected-->gray , these buttons movable grouping].
so when writing linq logic,how can know on criteria have group in particular way?
for ex: have columns b c d e f.in this, can choose group or b or b or acb....etc. possible combinations.how achieve this?i don't want if else check because have many possibilities.if 1 more filter added,it have many more possibilities. that's why thinking need of general approach this.
edit 2:
please find attachment , how trying below.
//for following ,i need way of writing passing right values var reportgroupingcp = (from t in taskentries group t new { clientid,projectid } g select new { clientid = g.key.clientid, projectid = g.key.projectid, result = (type)g //what t }).tolist(); var reportgroupingce = (from t in taskentries group t new { clientid,employeeid } g select new { clientid = g.key.clientid, employeeid = g.key.employeeid, result = (type)g //what t }).tolist(); //can't use above if there filter client.similarly other cases/i don't want write each one.i need way dynamically.may passing enum or adding ids class or else
if understood question correctly, wan't group data dynamically on multiple properties.
the easiest solution use dynamic linq lets create queries strings can compose user inputs.
// userselections created dynamically user selected. var userselections = new[] { "clientid", "projectid" }; var propertiestogroupby = string.join(", ", userselections); var groupeddata = data.groupby("new(" + propertiestogroupby + ")");
it's not type safe nor checked during compile time, easy use , solves problem. it's documented nicely.
i tried come solution dynamically combines expression<func<taskentry, object>>
s got stuck on creating expression
ìnstantiates anonymous type use inside groupby(new { ... })
. because number of selected properties group not known during compile time, it's not possible create anonymous type.
Comments
Post a Comment