0
讓說,我有以下模式跨越多對多關係的LinqToSql查詢?
Content(Id, ....)
TagContent(TagId, ContentId)
Tag(TagId, Name)
假設我想選擇一個有名字的「測試」標籤的所有內容記錄。
在SQL我會寫:
select Content.Id
from Content
join TagContent as TC on (TC.ContentId = Content.Id)
Join Tag on (TC.TagId = Tag.Id)
where Tag.Name = 'Test'
你可以建議如何,如果您有隻寫表Linq中類似的查詢? (我想創建一個擴展方法Content.ByTag(「標籤」) - > IQueryable的)
我只設法創建使用SQL語句exists
代替join
查詢。 這意味着查詢效率極低。
我目前低效的解決方案如下所示:
DataContext.Contents.Where(c => c.TagContents.Any(tc => tc.Tag.Name == "Test"))
注: ,因爲我想使擴展方法上DataContext.Contents我將無法訪問其他表是的DataContext .Tag和DataContext.ContentTag。
在我的分機menthod我沒有訪問TagContents和標籤 – 2009-10-30 15:57:13
啊我看到你的問題。 – 2009-10-30 16:08:18