2012-09-28 35 views
0
var Query2 = from x in CoreDatabase.Set<tblPerson>() 
       .Include(a => a.tblApplicationInterface.Select(b => b.tblApplicationName) 
       .Where(c => c.AppplicationName == "MshHumanResources")) 

       select x; 

找引入路徑表達式必須引用的類型定義的導航屬性。對於參考導航屬性使用虛線路徑,對集合導航屬性使用Select運算符。 我只是想寫新手對LINQ to SQL的lambda表達式,其中後兩個語句包括

這裏一些作品:

Select * 
From tblPerson a INNER JOIN tblApplicationInterface b 
on a.id = b.id 
INNER Join tblApplicationName c 
ON b.fkid=c.id 
Where b.ApplicationName like 'MshHumanResources' 
+0

如果你想要做那個sql,你爲什麼選擇了tblPerson集? –

回答

2

這不是如何Include作品。你想用join代替:

var Query2 = from a in CoreDatabase.Set<tblPerson>() 
      join b in CoreDatabase.Set<tblApplicationInterface>() on a.id equlas b.id 
      join c in CoreDatabase.Set<tblApplicationName>() on b.fkid equals c.id 
      where c.AppplicationName == "MshHumanResources" 
      select a; 

這是剛剛從選擇列 - 如果你想從其他表選擇列可以創建一個類來組合你需要的字段或使用匿名類型。

+0

如果我刪除where語句,我得到一個內部連接查詢,上面的語句...... – hidden

+0

我想這是一個非常好的答案。 – hidden

0

試試這個:

var Query2 = CoreDatabase.Set<tblPerson> 
       .Include("tblApplicationInterface") 
       .Include("tblApplicationInterface.tblApplicationName") 
       .Where(x => x.ApplicationName == "MshHumanResources"); 
+0

我似乎不能夠.include(x => x.tblApplicationInterface.tblApplicationName)那行 – hidden

+0

也許Linq to SQL不允許lambda表達式作爲包含參數。我改變了我的答案使用文字。 –

0

直接,你可以說

var people = CoreDatabase.Set<tblPerson>().Where(p => 
p.tblApplicationInterface.tblApplicationName.ApplicationName == "MshHumanResources"); 

如果我正確理解你的導航性能。這將選擇應用界面的應用名稱爲MshHumanResources的用戶。

+0

但它不會加載來自tblApplicationIterface和tblApplicationName的數據 –

+0

是的,那時你只有一個tblPerson對象。如果你想要導航屬性也填入,你需要包含在你的答案。我認爲這裏的where子句更精確,但是如果我正確閱讀導航屬性並記住include作品。 –

+0

其實,我認爲如果你引用它們,linq到sql會填充它們。但是,我記得include包含一個字符串參數,而不是lambda,所以我不完全確定它引用的是什麼ORM。 –