2012-02-20 74 views
0

我得到一個表Peoples和一個表PeopleRequirements用LINQ查詢兩個SQL表

PeopleRequirements.PeopleId被分配了FK約束到Peoples.Id並且還包含一個位(布爾值)字段PeopleRequirements.IsActive

現在我想查詢到其在PeopleRequirements一個行存在(其中行存在,等於PeopleId == People.Id)和PeopleRequirements.IsActivetrue所有的人。

如何用EF4和LINQ實現這一目標?

我已經使用NavigationProperties嘗試:

e.QueryableSource = _dataContext.Peoples.Where(a => a.EMail != string.Empty && a.EMail != null && a.PeopleRequirements.Count > 0);

+0

噢,是的,對 - 會加上這個。秒 – SeToY 2012-02-20 11:36:44

+0

是人:人要求1:1或1:很多? – BlueChippy 2012-02-20 11:55:07

+0

這是一個1:1的關係 – SeToY 2012-02-20 11:57:15

回答

1

這通常不是你會怎麼做事情EF,通常你會使用導航屬性在模型的兩個實體聯繫起來。話雖如此,如果他們沒有以正常的方式聯繫起來,你會使用linq。

from pplReq in PeopleRequirements 
from person in People 
where pplReq.PeopleId == person.Id 
where pplReq.IsActive 
select pplReq; 

編輯:根據各地的導航屬性的更新,你可以使用

from pr in _dataContext.PeopleRequirements 
where pr.People != null 
where pr.IsActive 
select pr 

這將發現這是積極的,並鏈接到一個實際的人

編輯所有PeopleRequirements:繼承人的交談情況下,附有主動要求的人

from person in _dataContext.Peoples 
from req in person.PeopleRequirements 
where req.IsActive 
select distinct person 

即時通訊不太確定您是否需要區分。

+0

謝謝你的回答 - 我編輯了我的帖子,以澄清確實存在NavigationProperties :) – SeToY 2012-02-20 11:38:28

+0

嗯,我可能會錯過某些東西,但不會取得PeopleRequirements而不是人民有附加條件? – SeToY 2012-02-20 11:47:58

+0

是的,你確實想要它嗎?附加主動要求的人員? – 2012-02-20 11:58:19