c# - Linq lambda entities , does not contain definition -


i have query code here :

//get records based on activityid , taskid. public ilist<model.questionhint> getrecords1(int listtask, int listactivity) {     ilist<model.questionhint> lstrecords = context.questionhints.tolist();     return lstrecords.groupby(x => new { x.questionno, x.activityid, x.taskid  }).where(a => a.taskid == listtask && a.activityid == listactivity).tolist();  } 

the error lies in .where statement, says not contain definition activityid , taskid.

full error :

'system.linq.igrouping' not contain definition 'activityid' , no extension method 'activityid' accepting first argument of type 'system.linq.igrouping' found (are missing using directive or assembly reference?)

i weak in query statements, want retrieve records database activity id = , task id = , group them questionno, activityid , task id .

the simplest fix here is: filter (where) before group; reduce work grouping has do:

return context.questionhints     .where(a => a.taskid == listtask && a.activityid == listactivity)     .groupby(x => new { x.questionno, x.activityid, x.taskid  })     .tolist(); 

the reason isn't working in original code is, mentioned, groupby returns sequence of groups - each of has .key (your anonymous type) , ienumerable<t> sequence of items in group.

however! method claims return ilist<model.questionhint>; grouped data not, , never be, ilist<model.questionhint> - ilist<igrouping<{some anonymous type, model.questionhint>>. so: cannot group if claiming ilist<model.questionhint> - , since grouping anonymous type, can't change return type match. have 2 choices:

  • don't group
  • group declarable (a custom type, or tuple<...>), , change return type match

for example:

public ilist<igrouping<tuple<int,int,int>,model.questionhint>>         getrecords1(int listtask, int listactivity) {     return context.questionhints         .where(a => a.taskid == listtask && a.activityid == listactivity)         .groupby(x => tuple.create(x.questionno, x.activityid, x.taskid))         .tolist();  } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -