Asp.net 实体查询帮助

Asp.net 实体查询帮助,asp.net,vb.net,entity-framework,linq-to-entities,entity-framework-4,Asp.net,Vb.net,Entity Framework,Linq To Entities,Entity Framework 4,我似乎不知道如何正确地编写这个查询。我试过各种组合,但都没用 以下是我的数据库模型的相关部分: 我需要选择与给定类别和组匹配的产品,以及与给定年份、品牌、型号、子型号匹配的产品。我已经做了以下工作: ItemList = From P In gDataContext.Products.Include("Groups").Include("Groups.Category1").Include("LookupYearMakeModels") From G In P.Groups Where G.

我似乎不知道如何正确地编写这个查询。我试过各种组合,但都没用

以下是我的数据库模型的相关部分:

我需要选择与给定类别和组匹配的产品,以及与给定年份、品牌、型号、子型号匹配的产品。我已经做了以下工作:

 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
我现在还必须选择与类别和组匹配但通用的产品(Product.Univeral=True)

我目前正在写两个查询,上面一个和下面一个。我通过简单地使用ItemList.AddRange(ItemList2)来合并这两个结果

但我希望将两个查询合并为一个查询,并避免合并结果。我怎么做

谢谢你的帮助

您可以使用


我试图建立一个类似的模型,我相信这是可行的。在这里,我选择的产品组与给定的类别和组匹配,并且具有匹配的年份/品牌/型号/子型号,或者是通用的

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

我很感谢你的回答,但我正在寻找一种方法,将两个查询合并为一个查询,而不仅仅是合并结果。效果非常好。谢谢。@mga911,太好了,很高兴它起作用了。这是否意味着我得到了赏金:)?是的,我将你的答案标记为接受答案。如果它还没有给你积分,那么你很可能会在明天开放的悬赏期结束时得到积分。谢谢,阿加尼不得不点击悬赏来奖励它。我想你现在有了它。我会避免使用保留词,比如Group,作为我的一个对象的名称。
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)
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