2

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: alt text

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 = 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!

1
  • 1
    I would stay away from using reserve words like Group as a name for one of my objects. Commented Oct 20, 2013 at 18:13

2 Answers 2

2
+50

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

Sign up to request clarification or add additional context in comments.

3 Comments

@mga911, great, glad it worked. Does this mean I get the bounty :) ?
Yes and I marked your answer as the accepted answer. If it didn't credit you the points yet then you'll probably get them when the open bounty period ends tomorrow. Thanks again
I had to click on the bounty to award it. I think you have it now.
0

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)

1 Comment

I appreciate the answer but I'm looking for a way to make the two queries into one query not just merged the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.