2017-01-31 14 views
1

これは正常に動作ラムダ式と私のLINQクエリです:複数テーブルのLINQクエリでOrderBy句を追加するにはどうすればよいですか?

var query = this.context.Blocks.Where(o => 
    o.IsActive && o.ProductSizes.Any(x => 
     x.SectionProductSizes.Any(y => 
      && y.SectionID == queryCriteria.SectionId y.Section.SizeTypeID == o.SizeTypeID 
     ) 
    ) 
); 

我々はSectionProductSizesテーブル内DisplayOrder列を上の仕分け追加することができますどのように?このために使用

追加モデルクラス:

public partial class Blocks 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public int SizeTypeID { get; set; } 
    public int DisplayOrder { get; set; } 
    public virtual ICollection<ProductSize> ProductSizes { get; set; } 
} 

public partial class ProductSize 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public int ProductID { get; set; } 
    public int ProductSizeID { get; set; } 
    public int DisplayOrder { get; set; } 
    public virtual Product Product { get; set; } 
    public virtual ICollection<SectionProductSize> SectionProductSizes { get; set; } 
} 

public partial class SectionProductSize 
{ 
    public int SectionProductSizeID { get; set; } 
    public int SectionID { get; set; } 
    public int ProductSizeID { get; set; } 
    public int DisplayOrder { get; set; } 
    public virtual Section Section { get; set; } 
} 

public class Section 
{ 
    public int ProductID { get; set; } 
    public int SizeTypeID { get; set; } 
    public int DisplayOrder { get; set; } 
} 
+1

モデルクラスも –

+0

あなたのブロックは、多くの 'ProductSize'を含むことができ、自分の順番のものは'多くを含むことができますSectionProductSize'では、注文をどのように決定するのですか? –

答えて

0

それはあなたのモデルを見ることなく、この質問に答えることは難しいですが、あなたが提供したものから次のことがうまくいけば動作するはずです。

var query = this.context.Blocks.Where(o => 
         o.IsActive && o.ProductSizes.Any(x => 
         x.SectionProductSizes.Any(y => 
         && y.SectionID == queryCriteria.SectionId y.Section.SizeTypeID == o.SizeTypeID 
         ) 
        ) 
        ).OrderBy(x => x.SectionProductSizes.DisplayOrder); 
+0

LINQクエリに使用されるモデルクラスが追加されました。 –

0

私は、通常のLINQクエリにラムダ式のクエリを変換することによって、問題を解決:

 var query = from bl in this.dbContext.Blocks 
        join ps in this.dbContext.ProductSizes on bl.Id equals ps.ModularContentBlockID 
        join sp in this.dbContext.SectionProductSizes on ps.Id equals sp.ProductSizeID 
        join se in this.dbContext.Sections on sp.SectionID equals se.Id 
        where bl.IsActive 
         && se.SectionSizeTypeID == bl.SectionSizeTypeID 
         && sp.SectionID == queryCriteria.SectionId 
        orderby sp.DisplayOrder 
        select bl; 
関連する問題