我想对sql查询进行linq查询
我的linq查询就像那样
var query3 = from p in _productRepository.Table
join bs in query2 on p.Id equals bs.ProductId
orderby p.ProductTags.Count(y=>y.Id==locationId)>0
select p;
如何在sql查询中实现此 p.ProductTags.Count(y => y.Id == locationId)> 0 。
答案 0 :(得分:1)
如果我理解你的LINQ查询正在做什么,它会先排序所有ProductTags
中的相关计数首先大于0,然后是其余的,但不按计数排序。在这种情况下,以下查询中的ORDER BY
将类似:
Select p.*
From ProductRepository As p
Join <query2> As bs On p.ID = bs.ID
Order By Case When (
Select Count(*)
From ProductTags As y
Where y.ProductID = p.ID
And y.ID = @locationID) > 0 Then 1 Else 0 End;
答案 1 :(得分:0)
SELECT productRepository.Id FROM _productRepository.Table
JOIN *secondtable*
WHERE productRepository.Id= *secondtable*.ProductId
AND productRepository.Id = locationId
Group By by productRepository.Id
Having Count(*) > 0
ORDER BY COUNT(*)
从Id获取其他详细信息。