2011-06-20 14 views
3

私はこのProductsテーブルのFacetTypesに基づいて、そのFacetでグループ化する必要があるLinqクエリを作成しようとしています。LinqとEntityFramework 4ネストされたサブクエリで複数の内部結合がある

これは、テーブルの構造である:私はfacetTypeIdsの配列を渡される enter image description here

、彼らはに基づいて結合するクエリに構築する必要がある9、6、52

FacetTypeId 9 has a name of "160" and is a Facet of "Size"  
FacetTypeId 6 has a name of "157" and is a Facet of "Size"  
FacetTypeId 52 has a name of "Cool Brand" and is a Facet of "Brand" 

言います面、次のように:

select * from products p 
inner join (select productId from productFacets where facetTypeId in (9, 6)) 
    p1 on p1.productId = p.productId 
inner join (select productId from productFacets where facetTypeId in (52)) 
    p2 on p2.productId = p.productId 

結果は言う結果セットは次のとおりです。

私を取得するブランド "クールブランド"とサイズ(160または157)の製品

これを動的に構築するlinqクエリを作成するにはどうすればよいですか?

これはlinqでどのように形成されるのかちょっと固まっています。

EDIT:

これは私が一種のそれはかなり非効率的な感じがして作ってみたコードです。

MyDbContext _context; 

// Groups FacetTypeIds by Facet into int lists 
Dictionary<int, List<int>> createFacetGroup(int[] facetTypeIds) 
{ 
    var facets = new Dictionary<int, List<int>>(); 

    var facetTypes = from ft in _context.FacetTypes where facetTypeIds.Contains(ft.FacetTypeId) select ft; 
    foreach (var facetType in facetTypes) 
    { 
     if (facets.ContainsKey(facetType.Facet.FacetId)) 
      facets[facetType.Facet.FacetId].Add(facetType.FacetTypeId); 
     else 
      facets.Add(facetType.Facet.FacetId, new List<int> { facetType.FacetTypeId }); 
    } 

    return facets; 
} 

public List<Product> FindProductsByGroupedFacetTypeIds(int[] facetTypeIds) 
{ 
    var groupedFacetTypeIds = createFacetGroup(facetTypeIds); 

    // this seem very inefficient but ToList needs to be called 
    // otherwise the results products in the foreach loop dont end 
    // up with the correct result set 
    var products = _context.Products.ToList(); 

    foreach (var facetTypeIdGroup in groupedFacetTypeIds) 
    { 
     var facetTypeIdGroupArray = facetTypeIdGroup.Value.ToArray(); 
     products = (from p in products where p.FacetTypes.Any(x => facetTypeIdGroupArray.Contains(x.FacetTypeId)) select p).ToList(); 
    } 

    return products; 
} 

答えて

0

を、私はこのために私の解決策を提出します。

これはきれいではありませんが、機能します。誰がより良い解決策を持っている場合、私はそれを

MyDbContext _context; 

// Groups FacetTypeIds by Facet into int lists 
Dictionary<int, List<int>> createFacetGroup(int[] facetTypeIds) 
{ 
    var facets = new Dictionary<int, List<int>>(); 

    var facetTypes = from ft in _context.FacetTypes where facetTypeIds.Contains(ft.FacetTypeId) select ft; 
    foreach (var facetType in facetTypes) 
    { 
     if (facets.ContainsKey(facetType.Facet.FacetId)) 
      facets[facetType.Facet.FacetId].Add(facetType.FacetTypeId); 
     else 
      facets.Add(facetType.Facet.FacetId, new List<int> { facetType.FacetTypeId }); 
    } 

    return facets; 
} 

public List<Product> FindProductsByGroupedFacetTypeIds(int[] facetTypeIds) 
{ 
    var groupedFacetTypeIds = createFacetGroup(facetTypeIds); 

    // this seem very inefficient but ToList needs to be called 
    // otherwise the results products in the foreach loop dont end 
    // up with the correct result set 
    var products = _context.Products.ToList(); 

    foreach (var facetTypeIdGroup in groupedFacetTypeIds) 
    { 
     var facetTypeIdGroupArray = facetTypeIdGroup.Value.ToArray(); 
     products = (from p in products where p.FacetTypes.Any(x => facetTypeIdGroupArray.Contains(x.FacetTypeId)) select p).ToList(); 
    } 

    return products; 
} 
1

あなたのエンティティモデル名は例えばYourEntitieであれば、この を試してみてください:この質問を閉じるの利益のために

YourEntitie urEntity = new YourEntitie(); 
List<Products> prdList = (from pro in urEntity.Products.Include("FacetTypes") 
         where (pro.FacetTypes.Where 
            (fac => fac.FacetTypeID == 9 || 
            fac => fac.FacetTypeID == 6).Count() > 0) 
           && (pro.FacetTypes.Where 
             (fac => fac.FacetTypeID == 52).Count() > 0) 
         select pro).ToList(); 
+0

歓声を見てみたい、私はいくつかの微調整の後に動作するようにあなたのクエリを得たが、どのように私は動的に与えられたfacetTypeIds配列に基づいて、そのクエリを構築するのでしょうか? –

+0

私はあなたが2アレイの権利をしたいと思いますか? OR条件用AND条件AND またはSIZE用とブランド用 の両方に対応していますか? – AlaaL

+0

配列には多くのidを入れることができます。したがって、サイズ、ブランド、色、形、重量などのようなファセットの量は分かりません。 –

関連する問題