简体   繁体   English

LINQ 查询 - 如何使用 map 将一个结果集转换为另一个 object 使用 ZE0626222614BDEEE319FF51D8EEZ4

[英]LINQ Query - How to map a resultset into another object using Select

I have an object hierarchy arranged as Continents > Countries > Cities.我有一个 object 层次结构,排列为大洲 > 国家 > 城市。 Am able to select all the cities in a specific "country" as below.我能够 select 如下特定“国家”的所有城市。 What I am looking for is a way to merge these two queries, and arrive at the cityList in a single query.我正在寻找一种方法来合并这两个查询,并在一个查询中到达 cityList。

var cities = network.Continents
    .SelectMany(continent => continent.Countries)
    .Where(ctry => ctry.Id == "country")
    .SelectMany(ctry => ctry.Cities);

List<City> cityList = (from c in cities
                        select new City { Id = c.Id, Name = c.Name }).ToList<City>();

The "c in cities" have a different structure from the one in cityList and hence the mapping in the second query. “城市中的 c”与 cityList 中的结构不同,因此在第二个查询中映射。

Just use dot notation in your query:只需在查询中使用点符号:

var cities = network.Continents
    .SelectMany(continent => continent.Countries)
    .Where(ctry => ctry.Id == "country")
    .SelectMany(ctry => ctry.Cities)
    .Select(cty=> new City{Id = cty.Id, Name = cty.Name }).ToList<City>();

I think it's readable, and it doesn't have extra overhead;我认为它是可读的,并且没有额外的开销; usually, the generated SQL query is similar to what you may write on your own, hence the readability of this one is its advantage.通常,生成的 SQL 查询与您自己编写的查询相似,因此该查询的可读性是其优势。

You should be able to do just this:你应该能够做到这一点:

var cityList = network.Continents
    .SelectMany(continent => continent.Countries)
    .Where(ctry => ctry.Id == "country")
    .SelectMany(ctry =>
        ctry.Cities.Select(c => new City { Id = c.Id, Name = c.Name })
    ).ToList();

Alternatively:或者:

var cityList =
    (from continent in network.Continents
     from country in continent.Countries
     where country.Id == "country"
     from city in country.Cities
     select new City { Id = city.Id, Name = city.Name })
    .ToList();

Another alternative to the options posted:发布选项的另一种选择:

var cityList = network.Continents
                      .SelectMany(continent => continent.Countries)
                      .Where(ctry => ctry.Id == "country")
                      .SelectMany(ctry =>  ctry.Cities,
                                  c => new City { Id = c.Id, Name = c.Name })
                      .ToList();

This overload of SelectMany (in the second call) is the one used by the C# compiler in query expressions. SelectMany的这种重载(在第二次调用中)是 C# 编译器在查询表达式中使用的重载。 Note that if you want to write it as a query expression, you can do so easily:请注意,如果您想将其编写为查询表达式,您可以轻松地这样做:

var cityList = (from continent in network.Continents
                from country in continent.Countries
                where country.Id == "country"
                from city in country.Cities
                select new City { Id = city.Id, Name = city.Name }).ToList(); 

In LINQ to Objects the query expression will be slightly less efficient than the dot notation form in this particular case, because the continent and country range variables will be propagated down to the select clause... but I'd expect the SQL generated by any database LINQ provider to be the same, and even within LINQ to Objects the difference is likely to be negligible.在 LINQ 到 Objects 中,查询表达式在这种特殊情况下的效率将略低于点表示法形式,因为大陆和国家范围变量将向下传播到 select 子句......但我希望 Z9778882876 生成的 Z9778882876数据库 LINQ 提供者要相同,即使在 LINQ 到 Objects 内,差异也可能可以忽略不计。

Note that you don't need to specify the type argument when calling ToList - the type will be inferred as City already.请注意,调用ToList时不需要指定类型参数 - 类型将被推断为City

Try the following:尝试以下操作:

var cities = 
    from continent in network.Continents
    from country in continent.Countries
    from city in country.Cities
    where country.Id = "country"
    select new City { Id = c.Id, Name = c.Name };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM