【问题标题】:Linq: How to make an object from linq resultLinq:如何从 linq 结果制作对象
【发布时间】:2016-08-27 01:02:50
【问题描述】:

我有 3 张桌子 Continent、Country 和 City。我需要制作一个有国家和国家有城市的大陆类型的对象。

大陆 --> 国家 --> 像这样的城市。欧洲 --> 德国 --> 法兰克福、柏林等。

我的代码:

public partial class Continent
{
    public Continent()
    {
        this.Countries = new HashSet<Country>();
    }

    public int ContinentId { get; set; }
    public string ContinentName { get; set; }

    public virtual ICollection<Country> Countries { get; set; }
}

public partial class Country
{
    public Country()
    {
        this.Cities = new HashSet<City>();
    }

    public int CountryId { get; set; }
    public string CountryName { get; set; }
    public Nullable<int> ContinentId { get; set; }

    public virtual ICollection<City> Cities { get; set; }
    public virtual Continent Continent { get; set; }
}

public partial class City
{
    public int CityId { get; set; }
    public string CityName { get; set; }
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}

var Result = (from a in db.Continents
              join b in db.Countries
                on a.ContinentId equals b.ContinentId
              join c in db.Cities on b.CountryId equals c.CountryId
              where a.ContinentId == 1
              select new
                     {
                         ContinentName = a.ContinentName,
                         CountryName = b.CountryName,
                         CityName = c.CityName
                    });

这是结果:

我需要创建一个包含自己的县市的大陆类型的对象

【问题讨论】:

  • 我需要制作一个对象你的问题是什么?

标签: c# entity-framework linq


【解决方案1】:

使用命名空间System.Entity.Data;

var continent = db.Continents
                  .Include(c => c.Countries.Select(cn => cn.Cities))
                  .FirstOrDefault(c => c.ContinentId == 1);

【讨论】:

  • 谢谢 :) ...我完全按照你说的做了,但是当我查看生成的 t-sql 时,我发现了很多连接和无用的语句
  • 此查询将使用国家和城市表加入大陆。
  • 我不会在关系数据库查询中称 JOIN 语句无用...
  • 只有在关闭数据库连接后仍需要保留对其他类型的引用并且不将结果存储在其他位置时才需要包含。
  • @Sahuagin - OP 已明确表示也要获取关系数据。这就是为什么我急于加载相关实体;作为 OP neds 国家和城市的详细信息,如果我们进行延迟加载,最终会导致多个数据库命中。
【解决方案2】:

要获得您表示的结果,您可以执行以下操作:

using (var context = new MyContext()) {
   var data =
      context
      .Continents
      .SelectMany(continent =>
         continent
         .Countries
         .SelectMany(country =>
            country
            .Cities
            .Select(city =>
               new {
                  ContinentName = continent.Name,
                  CountryName = country.Name,
                  CityName = city.Name
               }
            )
         )
      ).ToList();

   // do something with data
}

【讨论】:

  • .Select(city =&gt; 部分,continentcountry 不在范围内。这使用查询语法要容易得多。
  • @GertArnold 不,由于关闭,它们肯定在范围内。是的,使用查询语法可能看起来更好。
  • @Sahuagin,谢谢你的帮助:) ...我不是在询问我是在询问如何映射结果以使对象成为具有其县和市的大陆类型的对象,谢谢
  • @Lucy 不确定你的意思。 Continent 已经是“具有其国家和城市的Continent 类型的对象”。你得到一个错误或什么?使用Continent.Countries 获取国家,使用Country.Cities 获取城市。已经是这样了。
  • @Lucy 也许你对如何从数据库中提取数据感到困惑?您应该在某个地方有一个模型类,可以从中提取数据,如我上面的代码(示例中为MyContext,不确定您的代码是什么)。
猜你喜欢
  • 2020-03-26
  • 2015-08-14
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
相关资源
最近更新 更多