2017-02-09 31 views
-1

我無法將這個sql查詢寫入linq.can任何機構請help.thanks提前寫在自我條件的多個加盟LINQ

select catp.Name from Article art join Categorys cat on art.CategoryId=cat.CategoryId join Categorys catp 
on cat.ParentCategoryId=catp.CategoryId or cat.CategoryId = catp.CategoryId where ArticleId=24 
+0

請永遠只是發佈SQL,並要求轉換。至少顯示一個類模型,以便導航屬性和關聯的多樣性是可見的。此外,展示你自己的第一個努力。他們向我們澄清的比你想象的更多。 –

回答

0

你給的查詢是有點混亂。爲什麼你加入了Categorys貓,並寫入了連接條件「或cat.CategoryId = catp.CategoryId」。它會給出與以下查詢相同的結果:

select cat.Name from Article art 
join Categorys cat on art.CategoryId=cat.CategoryId 
where ArticleId=24 

現在應該很簡單,在上面的查詢中編寫一個Linq。如果要在篩選項目的categorys其中ParentCategoryId =類別ID,那麼就應該是:

select cat.Name from Article art 
join Categorys cat on art.CategoryId=cat.CategoryId 
where ArticleId=24 and cat.ParentCategoryId = cat.CategoryId 

的Linq:

var result = (from cat in Categories 
       join art in Articals 
        on cat.CategoryId equals art.CategoryId 
       where art.CategoryId == 24 && cat.CategoryId == cat.ParentCategoryId 
       select cat.Name).ToList()