asp.net - T-Sql to linq conversion issues (order by) -
i'm having bit of issue converting following t-sql statement linq (using 4.0 entity framework)
i'm getting
unable cast type 'system.linq.iorderedqueryable
1' type 'system.linq.iqueryable
1'. linq entities supports casting entity data model primitive types.
t-sql
select i.id , i.name inventory i.id in ( select top 5 applicationid lastviewed group applicationid, someuserid having someuserid = @someuserid order max(id) desc )
this have right (with linqer)
dim query iqueryable(of inventory) = d in ctx.inventories ((from e in ctx.lastvieweds _ group e _ e.applicationid, _ e.someuserid _ g = group _ dfaitedsid = user _ order g.max(function(p) p.id) descending _ select new { _ applicationid _ }).take(5)).contains(new {.applicationid = d.id}) _ select d
it crashes when line.
query.tolist()
thanks time.
your 'problem' in first line. query have specified returns iorderedqueryable(of inventory)
trying assign variable of type iqueryable(of inventory)
. aren't getting error until query.tolist()
deferred execution. can let compiler infer type if have 'option infer' set on in project , query like:
dim query = d in ctx.inventories (from e in ctx.lastvieweds _ group e _ e.applicationid, _ e.someuserid _ g = group _ dfaitedsid = user _ order g.max(function(p) p.id) descending _ select applicationid take 5 _ ).contains(d.id) _ select d
or can change type of query
iorderedqueryable(of inventory)
note: have works fine in linq sql, entities stricter when comes casting.
edit: alright, let's try dig little deeper this. tell me line blows following:
dim innerlist = (from e in ctx.lastvieweds _ group e _ e.applicationid, _ e.someuserid _ g = group _ dfaitedsid = user _ order g.max(function(p) p.id) descending _ select applicationid take 5).tolist() dim query = (from d in ctx.inventories innerlist.contains(d.id) _ select d).tolist()
don't forget pare results down bit if enumerating queries 'tolist()' much. types of both variables list(of inventory)
Comments
Post a Comment