我很難將一個複雜的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