I am new to LINQ and Entity Framework and I am stuck on a particular query which I am trying to write.
I have a database with the below structure. To keep it simple, I have just included the files and fields that are relevant to the problem.
Table: SCMPOFILE
Columns:
POKEY
PONUMBER
...
Table: SCMSKUFILE
SKUKEY
POKEY - Foreign Key on SCMPOFILE.POKEY
.....
Table: SCMSHPMAST
SHIPKEY
DELIVERYDATE
.....
Table: SCMSHIPPINGDETAIL
SHPDTLKEY
SHIPKEY - Foreign Key on SCMSHPMAST.SHIPKEY
POKEY - Foreign Key on SCMPOFILE.POKEY
SKUKEY - Foreign Key on SCMSKUFILE.SKUKEY
......
As you can see from the structure, 1 shipment Key can have multiple shipment Detail Keys
Similarly, each PO and sku can have multiple Shipment Keys associated with it.
I am trying to find all skus (SCMSKUFILEs) where SCMSHPMAST.DELIVERYDATE is not null. As I have defined the foreign keys, the model generated automatically brings the SCMSHIPPINGDETAILs with each SCMSKUFILE associated to it (and SCMSHPMAST attached to each SCMSHIPPINGDETAIL). I can do joins on all the individual files to get my desired results, but I want to use the LINQ and the model generated by entity framework for that.
I am trying to do something like this:-
Select all SCMSKUFILEs where SCMSHIPPINGDETAILs has SCMSHPMAST having DELIVERYDATE not equal to null.
Is this something which can be done using a single LINQ query? I have scratched my head a lot but I am not able to write the LINQ query.
I hope I have explained my problem properly. Please let me know if I have missed anything?
Thanks,
Abhi.
Have you tried something like this?
Not sure off the top of my head if it’d work worth a damn with the nested ANY’s, but worth a try.
You could also go from the other direction, something like this…