Featured post
Entity Framework: reload all child entities from query based on child entity attributes -
is there way elegantly load child entities of ef entity if entity instance loaded result of query on child entity attributes? here simple example of i'm asking:
first, simple data tables:
create table invoices ( invoiceid int identity(1000,1) not null, customer nvarchar(50) not null, invoicedate datetime not null, constraint pk_invoices primary key (invoiceid) ) create table invoiceitems ( invoiceitemid int identity(1,10) not null, invoicefk int not null, purchaseditem varchar(24) null, quantity decimal(10,2) null, itemprice money null, constraint pk_invoiceitems primary key (invoiceitemid), constraint fk_invoiceitems_invoice foreign key (invoicefk) references invoices (invoiceid) )
now, want query invoice table based on matching invoice item, show items each selected invoice regardless of whether matches criteria:
var qryorders = ordr in ctx.invoiceitems .include("invoice") ordr.purchaseditem == "buggy whips" select ordr; foreach (var ordritm in qryorders) { console.writeline("order " + ordritm.invoice.invoiceid + " contains buggy whips"); console.writeline("full contents of order:"); foreach (var itm in ordritm.invoice.invoiceitems) { //this show buggy whip item console.writeline(itm.purchaseditem); } }
what want show order items in each order contains "buggy whips", including items not buggy whips. if writing in sql, use where invoiceid in ()
clause subquery. question is: can entity framework make happen in elegant way? i've come hacks work, nothing feels right.
pretty confusing question - i'll attempt answer statement:
what want show order items in each order contains "buggy whips", including items not buggy whips.
i think need flip invoiceitems/invoice around:
var query = ctx.invoices.include("invoiceitems") .where(x => x.invoiceitems.any(y => y.purchaseditem == "buggy whips")) .select(x => x.invoices);
that query return collection of invoices
, have @ least one purchased item of "buggy whips".
if want return collection of invoices
all of purchased items "buggy whips", replace .any
.all
.
not sure if after.
a part confuses me this:
if writing in sql, use invoiceid in () clause subquery
invoiceid
on invoices
table - item name (purchaseditem) on invoiceitems
table - how work?
perhaps if wrote example sql query convert linq-entities.
- Get link
- X
- Other Apps
Comments
Post a Comment