2012-01-20 70 views
-1
SELECT * INTO #Loc1 
FROM LocaleStringResource 
Where LanguageId = 1 

SELECT * INTO #Loc2 
FROM LocaleStringResource 
Where LanguageId = 3 

SELECT Loc1.Id, Loc1.LanguageId, Loc1.ResourceName, Loc1.ResourceValue, 
     Loc2.Id, Loc2.LanguageId, Loc2.ResourceName, Loc2.ResourceValue 
FROM   #Loc1 AS Loc1 INNER JOIN 
         #Loc2 AS Loc2 ON Loc1.ResourceName = Loc2.ResourceName 

更新: 我有一個名爲 - LocaleStringResource的表。 列:Id,LanguageId,ResourceName,ResourceValue,如何將linq中的這個sql腳本轉換爲實體?

假設我的系統中有2種語言。

Id Language 
1 English 
3 Bangla 

Id LanguageId  ResourceName    ResourceValue 
1 1     Admin.Address    Address 
2 1     Admin.Phone     Phone 
51 3     Admin.Address    SpAddress 
51 3     Admin.Phone     SpPhone 

通過ResourceName我得到什麼值是選擇語言。所以從管理頁面我想通過ResourceName查看什麼是兩種語言的ResourceValue。

所以我需要一個返回類似這樣的結果集。

ResourceName   ResourceValue As EnglishText  ResourceValue As SpanishText 
Admin.Address   Address       SpAddress 
Admin.Phone   Phone        SpPhone 
+0

如果你需要一些幫助,你需要顯示你已經嘗試過的東西。 –

回答

1

實體框架不支持臨時表和SELECT INTO建設,所以你不能直接轉換。你必須仔細檢查你的應用程序的邏輯並修改它,以便它可以用於EF或者你必須將這些代碼包裝到存儲過程中並從EF調用它(btw。你必須使用列別名來獲得結果集,因爲列名與EF一起使用時不能相同)。

2
 var loc1 = Context.ObjectSet<LocaleStringResource>.Where(r => r.LanguageId == 1); 
     var loc2 = Context.ObjectSet<LocaleStringResource>.Where(r => r.LanguageId == 2); 
     var result = (
         from l1 in loc1 
         join l2 in loc2 on l1.ResourceName equals l2.ResourceName 
         select new{ 
          ResourceName = l1.ResourceName, 
          EnglishText = l1.ResourceValue, 
          SpanishText = l2.ResourceValue 
         } 
      ).ToList();