Linq和Entity Framework 4具有多个带有嵌套子查询的内连接

时间:2011-06-20 16:51:25

标签: c# linq entity-framework entity-framework-4

我正在尝试根据需要按其Facet分组的FacetTypes在此Products表上编写Linq查询。

这是表结构: enter image description here

我传递了一个facetTypeIds数组,比如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" 

需要将它们构造成一个基于facet连接的查询,如下所示:

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中形成这一点感到困惑。

修改

这是我提出的代码,但感觉非常低效。

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;
}

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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;
}