我在使用 Linq join/groupby/orderby 查询时遇到问题
I'm having trouble with a Linq join/groupby/orderby query
我有一些语法错误。
var productsForList = from p in db.vEUProducts
join pt in db.vEUProductTags on p.ProductId equals pt.ProductId
group p by p.ProductId into relevance
where toList.Contains(pt.TagId) && (relevance.Count() == toList.Count)
orderby relevance descending
select pt;
我收到错误:
'The name "pt" does not exist in the current context"
为什么?
指向toList.Contains(**pt**.TagId)
我只是添加了分组依据,现在找不到 pt
?
我错过了什么?
TagID 是主键。我有产品和标签。我想获得与列表中的标签匹配一定次数的产品。
我昨天的问题 可能会有帮助。
你可以试试:
var productsForList = from p in db.vEUProducts
join pt in db.vEUProductTags on p.ProductId equals pt.ProductId
where toList.Contains(pt.TagId)
group pt by pt.ProductId into relevance
where relevance.Count() == toList.Count
select relevance;
(我把 where
一分为二 where
)
但我不确定您要做什么...我不知道 TagId
是否是主键。你应该试着解释什么是 TagId
以及你想获得什么。
结果使用示例:
// Each obj is a group with key the ProductId and elements the tags matched
foreach (var obj in productsForList)
{
int productId = obj.Key;
foreach (var matchedTags in obj)
{
}
}
我有一些语法错误。
var productsForList = from p in db.vEUProducts
join pt in db.vEUProductTags on p.ProductId equals pt.ProductId
group p by p.ProductId into relevance
where toList.Contains(pt.TagId) && (relevance.Count() == toList.Count)
orderby relevance descending
select pt;
我收到错误:
'The name "pt" does not exist in the current context"
为什么?
指向toList.Contains(**pt**.TagId)
我只是添加了分组依据,现在找不到 pt
?
我错过了什么?
TagID 是主键。我有产品和标签。我想获得与列表中的标签匹配一定次数的产品。
我昨天的问题
你可以试试:
var productsForList = from p in db.vEUProducts
join pt in db.vEUProductTags on p.ProductId equals pt.ProductId
where toList.Contains(pt.TagId)
group pt by pt.ProductId into relevance
where relevance.Count() == toList.Count
select relevance;
(我把 where
一分为二 where
)
但我不确定您要做什么...我不知道 TagId
是否是主键。你应该试着解释什么是 TagId
以及你想获得什么。
结果使用示例:
// Each obj is a group with key the ProductId and elements the tags matched
foreach (var obj in productsForList)
{
int productId = obj.Key;
foreach (var matchedTags in obj)
{
}
}