在实体模型中实现层次结构

时间:2015-11-10 14:03:12

标签: c# asp.net-mvc entity-framework linq

我需要创建一个模型和方法,其中将填充相关模型的列表。

我的模型看起来像:

public class SearchHierarchyModel  
{
    public IList<Continent> Continent { get; set; }
    public IList<Country> Country { get; set; }
    public IList<City> City { get; set; }
}

我的方法应该做类似的事情:

public IList<SearchHierarchyModel> GetHierarchyFull(int Coninent_Id)
{
    //pseduocode now
    create lists of countries based on continent id
    create lists of cities based on countries id from the prev. step
    return a list of lists with relevant countries and cities
}

模型类

public class Contient
{
    public int Id { get; set; }
    public string Name { get; set; }
}   

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ContientId { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int CityId { get; set; }
}

也许有更好的方法来实现这种层次结构?任何想法如何做到这一点?也许模型应该看起来不同?

1 个答案:

答案 0 :(得分:2)

根据您的人际关系设置方式,可以做类似的事情。

public IList<SearchHierarchyModel> GetHierarchyFull(int Continent_Id)
{

    var continent = db.Continents.FirstOrDefault(c => c.Id == Continent_Id);

    //Get all countries with a specified ContinentId
    var countryList = db.Countries.Where(c => c.ContinentId == Continent_Id);

    //Get all cities that have a matching CountryId from any country in the first list.
    var cityList = db.Cities.Where(c => countryList.Any(cl => cl.Id == c.CountryId)).ToList();

    //We need to get the original countryList as a true list rather than a collection of entities.
    //If we had called ToList above, it would error out.
    //If we had called ToList in the ForEach loop, we also would have issues.
    var countryList2 = countryList.ToList();

    var searchList = new List<SearchHierarchyModel>
    {
        new SearchHierarchyModel()
        {
            Continent = new List<Continent> { continent },
            Country = countryList2,
            City = cityList
        }
    };
    return searchList;
}

现在有一些关于上述内容的评论。在我看来,您只需要一个特定大陆的国家及其城市列表。如果是这样的话,实体框架会让你更容易。

我会将我的核心模型更改为:

public class Continent
{
    public int Id { get; set; }
    public string Name { get; set; }

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

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ContinentId { get; set; }

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

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int CountryId { get; set; }

    public virtual Country Country { get; set; }
}

然后我将层次结构模型设置为:

public class SearchHierarchyModel
{
    public Continent Continent { get; set; }
    public Country Country { get; set; }
    public City City { get; set; }
}

现在我们可以将搜索功能更改为:

public IList<SearchHierarchyModel> GetHierarchyFull(int Continent_Id)
{

    var countries = db.Countries.Where(c => c.ContinentId == Continent_Id);
    var cities = db.Cities.Where(c => countries.Any(co => co.Id == c.Id));

    var searchList = new List<SearchHierarchyModel>();
    foreach (var item in cities)
    {
        var newItem = new SearchHierarchyModel
        {
            Continent = item.Country.Continent,
            Country = item.Country,
            City = item
        };
        searchList.Add(newItem);
    }
    return searchList;
}

现在,我们不是迭代列表列表,而是迭代所有可能返回值的列表。此外,通过修改模型,可以明确定义模型之间的关系。这意味着我可以更容易地引用它们并与之互动。

这是一个搜索方法,使用Linq反向遍历对象,切断foreach循环并删除所有额外的列表。

public IList<SearchHierarchyModel> GetHierarchyFull(int Continent_Id)
{
    var continent = db.Continents.FirstOrDefault(c => c.Id == Continent_Id);

    if (continent == null)
        return null;

    var searchList = (from item in continent.Countries
        from city in item.Cities
        select new SearchHierarchyModel
        {
            Continent = continent, 
            Country = item, 
            City = city
        }).ToList();

    return searchList;
}