2009-04-07 34 views
2

在我的模型中,我將Address作爲基類,MailingAddressEmailPhone作爲Address的子類。一個Person有一個地址,以便查詢得到誰擁有奧克蘭郵寄地址的人應該是這樣的:Linq查詢包括子類型

var peopleInOakland = from p in entities.DbObjectSet.OfType<Person>() 
          from a in p.Addresses.OfType<MailingAddress>() 
          where a.City == "Oakland" 
          select p; 

我怎麼也包括人的郵寄地址在我的查詢結果?我知道我應該使用Include,但我不確定如何在.Include參數中命名MailingAddress

在此先感謝

回答

2

您必須創建一個新的類型具有您正在查找的特定類型,像這樣:

var peopleInOakland = 
    from p in entities.DbObjectSet.OfType<Person>() 
    from a in p.Addresses.OfType<MailingAddress>() 
    where a.City == "Oakland" 
    select 
     new { Person = p, Addresses = p.Addresses.OfType<MailingAddress>() }; 
0

使用的理解表達的本地名稱只要選擇到一個匿名類型:

... 
select new { 
    Persion = p, 
    Address = a 
}; 
+0

很高興知道爲什麼這是低票。 – Richard 2009-04-07 23:06:03