c# - need help around IQueryable query result -
assuming following tables
person
- id
- name
personteam
- id
- person_id
- is_supervisor
- team_id
team
- id
timesheet
- id
- team_id
i obtain timesheets supervisor. got name of supervisor, need select team got supervisor role. select time sheet of teams.
i believe following query does
var alltimesheets = ctx.personteam.where(y => y.person.name == supervisor_name).where(x => x.is_supervisor == true).select(z => z.team).select(t => t.timesheet); afer operation cannot understand alltimesheets a
iqueryable<icollection<timesheet>> i expected more a
<icollection<timesheet>> or ienumrable.
then questions :
- why got kind of result ?
- how obtain timesheet[] got iqueryable < icollection < timesheet > > ?
why did kind of result ? expected more
icollection<timesheet>
an iqueryable<t> is ienumerable<t>. reason it's returning iqueryable can chain other methods orderby onto , project actual sql.
i realized you're asking. "flatten" collection of collections, use selectmany instead of 2 chained selects:
var alltimesheets = ctx.personteam .where(y => y.person.name == supervisor_name && y.is_supervisor == true) .selectmany(z => z.team, (z, t) => t.timesheet); the answer second question still applies:
how obtain
timesheet[]iqueryable<icollection<timesheet>>
(first of use first part change iqueryable<timesheet>)
you can call 1 of "conversion" methods toarray, tolist, "hydrate" query concrete type.
you can call "asenumerableto cast anienumerableto convert query linq-to-objects, has better support custom functions in sorts, filters, etc. note callingasenunerable` no immediately fetch objects, as collection in enumerated.
Comments
Post a Comment