我很难将一个复杂的sql查询重写为linq。非常复杂的linq
-- gets the SpecificationAttributeOptionIDs that are in the FilteredSpecs list, but don't match
SELECT * FROM #FilteredSpecs [fs]
WHERE [fs].SpecificationAttributeOptionID NOT IN
(
-- gets the SpecificationAttributeOptionIDs that match
SELECT psam.SpecificationAttributeOptionID
FROM dbo.Nop_Product_SpecificationAttribute_Mapping psam
WHERE psam.AllowFiltering = 1 AND psam.ProductID = 1
)
基本上我有什么,是一个名单FilteredSpec这是C#是一个int列表。我试图从FilteredSpec列表中获取所有包含所有属性选项的产品。
我已经试过这是(这给我有限的LINQ的知识显然是行不通的):
var query = from p in Products
where (p.NpProductSpecificationAttributes.Select(a => filters.Contains(a.NpSpecificationAttributeOption.SpecificationAttributeOptionId)).Count() == 0)
select p;
任何人都可以指导我在正确的方向吗?
哇。谢谢。你是一个linq天才:))他们都工作,并认为第二个是更快一点,清洁的SQL。我还没有想出如何测试NpProductSpecificationAttribute是否将AllowFiltering属性设置为true – 2010-12-14 19:04:41
我最终设法对该属性进行了测试。虽然我更容易。感谢您的解决方案。你是最棒的! – 2010-12-14 19:14:46
@Tudor Carean:我编辑了第二个答案来检查AllowFiltering属性,并摆脱了第一个答案(这会更复杂)。 – StriplingWarrior 2010-12-14 19:20:10