【问题标题】:Help with Entity Query实体查询帮助
【发布时间】:2011-05-05 23:06:51
【问题描述】:

我似乎无法弄清楚如何正确编写此查询。我尝试了各种组合,但没有任何效果。

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

我需要选择与给定类别和组匹配的产品,以及与给定年份、品牌、型号、子型号匹配的产品。这是我在下面完成的:

 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)

来合并两者的结果
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

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

感谢您的帮助!

【问题讨论】:

  • 我不会使用像 Group 这样的保留词作为我的一个对象的名称。

标签: asp.net vb.net entity-framework linq-to-entities entity-framework-4


【解决方案1】:

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

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,太好了,很高兴它成功了。这是否意味着我得到了赏金:)?
  • 是的,我将您的答案标记为已接受的答案。如果它还没有记入您的积分,那么您可能会在明天开放赏金期结束时获得积分。再次感谢
  • 我必须点击赏金才能授予它。我想你现在有了。
【解决方案2】:

您可以使用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)

【讨论】:

  • 感谢您的回答,但我正在寻找一种方法将两个查询合并为一个查询,而不仅仅是合并结果。
猜你喜欢
  • 2011-06-30
  • 2011-05-21
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-06
  • 2011-11-07
相关资源
最近更新 更多