我想轉換下面的查詢,從查詢語法(這是正常工作),以LINQ的語法,但我似乎無法得到它的權利:LINQ to SQL的遞歸問題
SELECT
[t1].[ID], [t1].[ParentID], [t1].[FolderName],
[t1].[Purpose], [t1].[IsSystem],t1].IsHidden],
[t1].[ChangeSrc], [t1].[SyncGUID], [t1].[CreationDBTimeStamp]
FROM [dbo].[FileStorageFolders] AS [t0]
INNER JOIN [dbo].[FileStorageFolders] AS [t1] ON ([t0].[ID]) = [t1][ParentID]
WHERE ([t1].[Purpose] = @p0)
AND ([t1].[FolderName] = @p1)
AND ([t0].[ParentID] IS NULL)
AND ([t0].[Purpose] = @p2)
我用這個LINQ語法,但結果始終爲空值:
private static FileStorageFolder GetCapsuleContentFolder(FileStorageDataContext db)
{ IQueryable<FileStorageFolder> source = (from dbfolder in db.FileStorageFolders
join dbsubfolder in db.FileStorageFolders on
new { ParentID = dbfolder.ID } equals
new { ParentID = Convert.ToInt32(dbsubfolder.ParentID) }
where
dbfolder.ParentID == null &&
dbfolder.Purpose == 'REPORT' &&
dbsubfolder.Purpose == 'LayoutFolder' &&
dbsubfolder.FolderName == 'LayoutReport'
select new
{
dbsubfolder.ID,
ParentID = (System.Int32?)dbsubfolder.ParentID,
dbsubfolder.FolderName,
dbsubfolder.Purpose,
dbsubfolder.IsSystem,
pqrentID2 = dbfolder.ParentID,
Purpose2 = dbfolder.Purpose
}) as IQueryable<FileStorageFolder>;
return source.Single();
}
每當我看到像這樣的問題時,我會問:爲什麼不能只調用一個存儲過程或將其創建爲視圖並使用LINQ來構建投影? –
非常感謝Ray ......但通過另一種瞭解朋友的方式可以提高我們處理問題的能力。那是對的嗎? –
如果你在查詢中放置斷點並查看原始SQL有什麼區別? – TheRealTy