c# - OrderBy selector fails while projecting anonymous type -


when using entity framework 5, why work?

var query = categories.select(c => new      {          products = c.products.orderby(p => p.name)      }); 

while won't?

func<product, string> selector = p => p.name; var query = categories.select(c => new      {          products = c.products.orderby(selector)      }); 

the thrown exception is: unsupported overload used query operator 'orderby'.

making selector expression<func<product, string>> won't work directly , not compile because c.products not iqueryable<t>. collection type implementing ienumerable<t>. enumerable.orderby not accept expression parameter, delegate.

but ef still needs expression, not delegate. trick use asqueryable() on navigation collection:

expression<func<product, string>> selector = p => p.name; var query = categories.select(c => new  {      products = c.products.asqueryable().orderby(selector)  }); 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -