Help with Entity Query
I can't seem to figure out how to write this query properly. I've tried various combinations but nothing's worked yet.
Below is the relevant portion of my database model:
I need to select the products that match a given Category and Group, and that match a given Year, Make, Model, submodel. This I've done below:
ItemList = From P In gDataContext.Products.Include("Groups").Include("Groups.Category1").Include("LookupYearMakeModels") From G In P.Groups Where G.Category = Cat And G.Grp = Group From Y In P.LookupYearMakeModels Where Y.Year = YMM.Year And Y.Make = YMM.Make And Y.Model = YMM.Model And Y.Submodel =开发者_Python百科 YMM.Submodel Select P
I now also have to select products that match the Category, and Group but are Universal (Product.Univeral = True).
I'm currently writing two queries, the one above and the one below. I'm merging the results of the two by simply using ItemList.AddRange(ItemList2)
ItemList2 = From P In gDataContext.Products.Include("Groups").Include("Groups.Category1") where P.Universal From G In P.Groups Where G.Category = Cat And G.Grp = Group Select P
But I want to combine both queries into one and avoid merging the results. How can I do it?
Thanks for your help!
I tried to set up a similar model, and I believe this works. Here I am selecting products that have a group matching the given category and group and that have a matching year/make/model/submodel or are universal.
ItemList = From P In gDataContext.Products.Include("Groups").Include("Groups.Category1").Include("LookupYearMakeModels")
Where P.Groups.Any(Function(G) G.Category = Cat And G.Grp = Group) _
And ( _
P.LookupYearMakeModels.Any(Function(Y) Y.Year = YMM.Year And Y.Make = YMM.Make And Y.Model = YMM.Model And Y.Submodel = YMM.Submodel) _
Or _
P.Universal = True _
)
Select P
HTH
You can use IQueryable.Union Method:
ItemList = (From P In gDataContext.Products
.Include("Groups.Category1")
.Include("LookupYearMakeModels")
From G In P.Groups
Where G.Category = Cat And G.Grp = Group
From Y In P.LookupYearMakeModels
Where Y.Year = YMM.Year
And Y.Make = YMM.Make And Y.Model = YMM.Model
And Y.Submodel = YMM.Submodel
Select P)
.Union(
From P In gDataContext.Products
.Include("Groups.Category1")
Where P.Universal
From G In P.Groups Where G.Category = Cat And G.Grp = Group
Select P)
精彩评论