c# - Linq 和 EntityFramework 4 具有多个内部连接和嵌套子查询

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

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

这是表结构: enter image description here

我收到了一组 facetTypeId,比如 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

结果是一个结果集,上面写着:

获取品牌为“Cool Brand”且尺寸为(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;
}

最佳答案

试试这个 例如,如果您的实体模型名称是 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();

关于c# - Linq 和 EntityFramework 4 具有多个内部连接和嵌套子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6414687/

相关文章:

c# - 不包含 "Add"的定义

c# - 将 C# 脚本解析为 Java

c# - 如果 Int32 只是 int 的别名,那么 Int32 类如何使用 int?

c# - 从多个字段的嵌套集合中选择不同的列表

c# - linq 到 xml。读。并分配给 ViewData..noob

c# - XDocument - 表达式帮助

c# - Entity Framework 对模型对象及其方法的澄清

c# - 是否可以通过反射获取局部变量?

c# - System.Diagnostics.Process 程序继续运行

.net - 如何使用部分类扩展 ADO.NET Entity Framework 对象?