2009-04-07 36 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