C# Linq和EntityFramework 4,具有多个内部联接和嵌套子查询
我正在尝试基于需要按方面分组的FacetType在此Products表上编写Linq查询 这是表结构: 我通过了一个FacetTypeID数组,比如9、6、52C# Linq和EntityFramework 4,具有多个内部联接和嵌套子查询,c#,linq,entity-framework,entity-framework-4,C#,Linq,Entity Framework,Entity Framework 4,我正在尝试基于需要按方面分组的FacetType在此Products表上编写Linq查询 这是表结构: 我通过了一个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
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查询来动态地构建它
我有点被困在林克如何形成这个
编辑:
这是我提出的代码,但是感觉效率很低
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;
}
MyDbContext\u context;
//按方面将FacetTypeId分组到int列表中
字典createFacetGroup(int[]FacetTypeId)
{
var facets=newdictionary();
var facetTypes=来自_context.facetTypes中的ft,其中FacetTypeId.Contains(ft.FacetTypeId)选择ft;
foreach(facetTypes中的变量facetType)
{
if(facets.ContainsKey(facetype.Facet.FacetId))
facets[facetType.Facet.FacetId]。添加(facetType.FacetTypeId);
其他的
添加(facetType.Facet.FacetId,新列表{facetType.FacetTypeId});
}
返回面;
}
公共列表FindProductsByGroupedFacetTypeId(int[]FacetTypeId)
{
var groupedFacetTypeId=createFacetGroup(FacetTypeId);
//这似乎效率很低,但需要调用ToList
//否则,foreach循环中的结果产品不会结束
//生成正确的结果集
var products=_context.products.ToList();
foreach(groupedFacetTypeId中的变量facetTypeIdGroup)
{
var facetTypeIdGroupArray=facetTypeIdGroup.Value.ToArray();
products=(来自products中的p,其中p.FacetTypes.Any(x=>facetTypeIdGroupArray.Contains(x.FacetTypeId))选择p.ToList();
}
退货产品;
}
试试这个
如果您的实体模型名称为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();
yourentie-ureentity=new yourentie();
List prdList=(来自urEntity.Products.Include中的pro(“FacetTypes”)
where(pro.FacetTypes.where
(fac=>fac.FacetTypeID==9||
fac=>fac.FacetTypeID==6.Count()>0)
&&(pro.facetypes.Where
(fac=>fac.FacetTypeID==52).Count()>0)
选择pro).ToList();
为了结束这个问题,我将提交我的解决方案
虽然不漂亮,但很管用。如果其他人有更好的解决方案,我很乐意看到
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;
}
MyDbContext\u context;
//按方面将FacetTypeId分组到int列表中
字典createFacetGroup(int[]FacetTypeId)
{
var facets=newdictionary();
var facetTypes=来自_context.facetTypes中的ft,其中FacetTypeId.Contains(ft.FacetTypeId)选择ft;
foreach(facetTypes中的变量facetType)
{
if(facets.ContainsKey(facetype.Facet.FacetId))
facets[facetType.Facet.FacetId]。添加(facetType.FacetTypeId);
其他的
添加(facetType.Facet.FacetId,新列表{facetType.FacetTypeId});
}
返回面;
}
公共列表FindProductsByGroupedFacetTypeId(int[]FacetTypeId)
{
var groupedFacetTypeId=createFacetGroup(FacetTypeId);
//这似乎效率很低,但需要调用ToList
//否则,foreach循环中的结果产品不会结束
//生成正确的结果集
var products=_context.products.ToList();
foreach(groupedFacetTypeId中的变量facetTypeIdGroup)
{
var facetTypeIdGroupArray=facetTypeIdGroup.Value.ToArray();
products=(来自products中的p,其中p.FacetTypes.Any(x=>facetTypeIdGroupArray.Contains(x.FacetTypeId))选择p.ToList();
}
退货产品;
}
干杯,经过一些调整后,我让您的查询正常工作了,但是如何基于给定的facetTypeIds数组动态构建该查询?我想您需要2个数组,对吗?一个用于或条件,另一个用于和条件,或者一个用于大小,另一个用于品牌,对吗?数组可以包含许多id。因此,将有一个uknown数量的方面,例如:大小,品牌,颜色,形状,重量等