【发布时间】: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