c# - Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type -


i working .net4.5 , vs2013, have query gets dynamic result db.

dynamic topagents = this._datacontext.sql(     "select t.create_user_id \"user\", sum(t.netamount) \"amount\" transactiondetail t t.update_date > sysdate -7 group t.create_user_id")     .querymany<dynamic>(); 

following statement fails compilation error cannot use lambda expression argument dynamically dispatched operation without first casting delegate or expression tree type without allowing me run it

topagents.tolist().select(agent => new {     user = agent.user != null ? string.format("{0}", agent.user).replace("corpntgb\\", "") : null,     amount = agent.amount }); 

while 1 foreach works fine.

var data = new list<list<object>>(); foreach (dynamic agent in topagents) {     data.add(new list<object>     {         agent.user != null ? string.format("{0}", agent.user).replace("corpntgb\\", "") : null,         agent.amount     }); } 

in eyes after topagents.tolist() interpreted equivalent, because explicitly state var data = new list<list<object>>(); second statement allowed compiler?

why doesn't compiler allow linq select, allows each`?

the problem topagents dynamic - tolist() call dynamic, , select. has issues that:

  1. you can't use lambda expressions dynamic calls this;
  2. dynamic calls don't find extension methods anyway.

fortunately, operations don't need dynamic because element type dynamic. use:

ienumerable<dynamic> topagents = ...; 

... or use var. both of should fine.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -