从LINQ创建一个对象列表select new [英] Creating a list of objects from LINQ select new

查看:1329
本文介绍了从LINQ创建一个对象列表select new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么第一个linq查询可以工作,但第二个没有?

  var locations = 
from路径位置在db.Table中< RouteLocation>()
在db.Table中的连接位置< Location>()on routeLocation.LocationId等于location.Id
其中functionalLocation.RouteId == routeId
选择new Location(){Id = location.Id,ParentId = location.ParentId,
Name = location.Name
};


var locations =
从db.Table中的routeLocation中获取< RouteLocation>()
在db.Table中加入位置< Location>()on routeLocation.LocationId equals location.Id
其中functionalLocation.RouteId == routeId
选择新的{Location = location};

第二个查询的编译器错误为:


无法隐式转换类型
'System.Collections.Generic.List<<<匿名类型:位置位置>'
到'System.Collections.Generic.List< ; Location>


我尝试将var声明为List of Location,但仍然得到相同的错误。是否有可能在第二个示例中使用该语法,而不是像第一个示例中那样指定每个属性?

解决方案

第二个查询通过 new {...} 创建一个新的匿名类型。因此, locations 的类型是 IEnumerable 这个匿名类型,你可能试图如果你想创建一个新的 Location的列表 List>,它会产生显示的错误。

/ code>对象,那么你需要在类 Location 中创建一个拷贝构造函数(即构造函数的签名为 Location(Location location )将给定的 Location 的所有字段复制到新的字段中,然后您可以将查询更改为以下内容:

  var locations = 
来自db.Table中的routeLocation< RouteLocation>()
加入db.Table中的位置< Location>( )on routeLocation.LocationId equals location.Id
where functionalLocation.RouteId == routeId
选择新位置(位置);

这产生一个 IEnumerable< Location> ,它可以被转换为 List< Locat ion> 通过 ToList()方法。


Why does the first linq query work, but the second one does not?

var locations =
    from routeLocation in db.Table<RouteLocation>()
    join location in db.Table<Location>() on routeLocation.LocationId equals location.Id
    where functionalLocation.RouteId == routeId
    select new Location() { Id = location.Id, ParentId = location.ParentId, 
                            Name = location.Name 
                          };


var locations =  
    from routeLocation in db.Table<RouteLocation>()
    join location in db.Table<Location>() on routeLocation.LocationId equals location.Id
    where functionalLocation.RouteId == routeId
    select new { Location = location };

The compiler error for the second query is:

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: Location Location>>' to 'System.Collections.Generic.List<Location>

I tried declaring the var as a List of Location instead, but still get the same error. Is it possible for me to use the syntax in the second example, instead of having to specify each property as in the first example?

解决方案

The second query creates a new anonymous type by new {...}. The type of locations is therefore an IEnumerable of this anonymous type which youre probably trying to cast into aList` which produces the shown error.

If you want to create a list of new Location objects, then you need to create a copy constructor in the class Location (i.e. a constructor with the signature Location(Location location) which copies all fields of the given Location into the new one. Then you can change your query to the following:

var locations =  
    from routeLocation in db.Table<RouteLocation>()
    join location in db.Table<Location>() on routeLocation.LocationId equals location.Id
    where functionalLocation.RouteId == routeId
    select new Location(location);

This yields an IEnumerable<Location> which can be converted to an List<Location> by means of the ToList() method.

这篇关于从LINQ创建一个对象列表select new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆