代码之家  ›  专栏  ›  技术社区  ›  Ralph Shillington

包含子类型的Linq查询

  •  2
  • Ralph Shillington  · 技术社区  · 15 年前

    Address 作为基类和 MailingAddress , Email Phone 作为地址的子类。A. Person 有一个地址,因此查询获得一个拥有奥克兰邮寄地址的人如下所示:

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

    我如何在我的查询结果中包括此人的邮寄地址?我知道我应该使用Include,但我不确定如何命名 .Include

    提前谢谢

    2 回复  |  直到 15 年前
        1
  •  2
  •   casperOne    13 年前

    您必须创建一个新类型,该类型具有您要查找的特定类型,如下所示:

    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>() };
    
        2
  •  0
  •   Richard    15 年前

    只需使用理解表达式的本地名称选择一个匿名类型:

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