有没有更好的和优化的方法来使用C#创建嵌套列表?

时间:2019-01-18 15:38:46

标签: c# .net-core

我想基于三个可枚举列表创建一个平面列表。这些列表的数据存储在我的数据库的三个表中:大陆,国家/地区,城市。 为此,我为每个实体获取了3个循环,因此,如果我添加另一个名为District的实体,则需要第四个循环,依此类推。因此,有一种方法可以优化我的代码,以使用LooKup或更干净的Linq语法并提高性能?请参阅下面的代码:

大陆

Id  Name            
1   NorthAmerica    
2   Europe

国家

Id  Name              ContinentId        
1   USA               1
2   CANADA            1
3   FRANCE            2
4   ENGLAND           2

国家

Id  Name              CountryId        
1   PARIS             3
2   MONTREAL          2
3   NEW YORK          1
4   LYON              3
5   LONDRES           4

在C#中,我从存储库中获取每个表的数据

var lists = new List<Select> { };

var continents = _unitOfWork.ContinentRepository.Get().
                 Select(x => new { id = x.Id, name = x.name });

var countries = _unitOfWork.CountryRepository.Get().
                Select(x => new { id = x.Id, continentId = x.ContinentId, name = x.name });

var cities = _unitOfWork.CityRepository.Get().
             Select(x => new { id = x.Id, countryId = x.CountryId, name = x.name });


if (continents.Count() > 0)
{
    foreach (var continent in continents)
    {
        lists.Add(new Select { Id = continent.id, Value = continent.name, Level = "first-level" });
        foreach (var country in countries.Where(x => x.continentId == continent.id))
        {
            lists.Add(new Select { Id =  country.id , Value = country.name, Level = "second-level" });
            foreach (var city in cities.Where(x => x.countryId == country.id))
            {
                lists.Add(new Select { Id = city.id , Value = city.name, Level = "third-level" });
            }
        }
    }
}
NORTHAMERICA
   USA 
     NEW YORK
   CANADA
     MONTREAL
EUROPE
  FRANCE
    LYON
    PARIS
  ...
The class attribute will add the spaces (indentation), so i need it.
[
    {
        "id": 1
        "value": "NorthAmerica",
        "class": "first-level"
    },
    {
        "id": 1
        "value": "USA",
        "class": "second-level"
    },
    {
        "id": 3
        "value": "NEW YORK",
        "class": "third-level"
    },
    {
        "id": 2
        "value": "CANADA",
        "class": "second-level"
    },
    {
        "id": 3
        "value": "MONTREAL",
        "class": "third-level"
    },
    {
        "id": 2
        "value": "Europe",
        "class": "first-level"
    },
    {
        "id": 1
        "value": "FRANCE",
        "class": "second-level"
    },
    {
        "id": 3
        "value": "PARIS",
        "class": "third-level"
    },
    {
        "id": 4
        "value": "LYON",
        "class": "third-level"
    },
    {
        "id": 4
        "value": "ENGLAND",
        "class": "second-level"
    },
        "id": 5
        "value": "LONDRES",
        "class": "third-level"
    },
]

1 个答案:

答案 0 :(得分:0)

我不知道为什么在那些有图书馆可以更好地处理诸如此类的日子里,你为什么要做类似的事情 EntityFramework和EntityWorker.Core

在Entityworker.Core中,只需一次选择即可轻松完成 这是您模块的示例

public class Continent {

  [PrimaryKey]
  public int Id { get; set; }

  public string Name { get; set; }

  public List<Country> Countries { get; set; }

}

public class Country {

  [PrimaryKey]
  public int Id { get; set; }

  public string Name { get; set; }

  [ForeignKey(typeof(Continent))]
  public int ContinentId { get; set; }

  public List<City > Cities { get; set; }

}

   public class City {

      [PrimaryKey]
      public int Id { get; set; }

      public string Name { get; set; }


      [ForeignKey(typeof(Country))]
      public int CountryId { get; set; }
  }

现在您要做的就是获取数据和loadChildren

using (var rep = new Repository())
   {
     // this will load all Continent and Countries and Cities all togather with a single call
    List<Continent> Continents = rep.Get<Continent>().Where(x=> x.Id == 54).LoadChildren().Execute(); 
   }

此处有关该库的更多信息 https://www.codeproject.com/Tips/1222424/EntityWorker-Core-An-Alternative-to-Entity-Framewo

Entityframework可以执行相同的操作,除了您可以通过Include()代替LoadChildren http://www.entityframeworktutorial.net/basics/context-class-in-entity-framework.aspx