查询返回匿名类型的列表,而需要值列表

本文关键字:列表 返回 类型 查询 | 更新日期: 2023-09-27 18:02:39

我正在使用LINQ执行一个自定义查询,它根据我的语句返回一个匿名类型列表:

            var currUnitFaster = (from unit in pacificRepo.Units
                                 join loc in pacificRepo.GeoLocations on new { unit.geo_location.Latitude, unit.geo_location.Longitude } equals new { loc.geo_location.Latitude, loc.geo_location.Longitude }
                                 join st in pacificRepo.UnitStatuses on unit.c_number equals st.c_number
                                 select new { 
                                    index = "0", 
                                    unit.c_number, 
                                    unit.serial_number, 
                                    unused = "0", 
                                    unit.ip_address, 
                                    unit.build_version,
                                    status = (st.status == Pacific.Domain.Entities.UnitStatus.Statuses.COMPLETE) ? "<font color='"green'">" + st.stage + "</font>" : "<font color='"red'">" + st.stage + "</font>", 
                                    location = (loc==null) ? "Unknown" : loc.city + ", " + loc.state_abbrv,
                                    unit.location_at_address,
                                    latitude = unit.geo_location.Latitude,
                                    longitude = unit.geo_location.Longitude
                                 }).ToList();
因此,列表中返回的每个元素都是匿名类型:
        currUnitFaster[0].ToString() ==> "{ index = 0, c_number = J0000014, serial_number = A2F0JA02, unused = 0, ip_address = 169.254.0.9, build_version = J1, status = <font color='"green'">Link</font>, location = San Jose, CA, location_at_address = FCC, latitude = 37.390791, longitude = -121.905775 }"

我想要一个值的列表,而不是匿名类型的列表。这样:

currUnitFaster.First()[0] ==> "0"
currUnitFaster.First()[1] ==> "J0000014"
currUnitFaster.First()[2] ==> "A2F0JA02"
...

最简单的方法是什么?我知道我可以简单地创建一个列表,然后遍历currUnitFaster中的每个元素,并手动将其添加到该列表中。我想知道是否有一些更干净的转换或基于铸造的方法来做到这一点。


Edit:我采纳了Selman的建议,将select new改为select new object[]然而,我现在得到以下异常试图运行查询:

Exception: {"Unable to cast the type 'System.Nullable`1[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types."}

查询返回匿名类型的列表,而需要值列表

select new更改为select new object[]并删除属性名称

编辑:看来EF是在抱怨演员类型。试试这个:
var currUnitFaster = (from unit in pacificRepo.Units
                      join loc in pacificRepo.GeoLocations on new { unit.geo_location.Latitude, unit.geo_location.Longitude } equals new { loc.geo_location.Latitude, loc.geo_location.Longitude }
                      join st in pacificRepo.UnitStatuses on unit.c_number equals st.c_number
                      select new 
                            { 
                                index = "0", 
                                unit.c_number, 
                                unit.serial_number, 
                                unused = "0", 
                                unit.ip_address, 
                                unit.build_version,
                                status = (st.status == Pacific.Domain.Entities.UnitStatus.Statuses.COMPLETE) ? "<font color='"green'">" + st.stage + "</font>" : "<font color='"red'">" + st.stage + "</font>", 
                                location = (loc==null) ? "Unknown" : loc.city + ", " + loc.state_abbrv,
                                unit.location_at_address,
                                latitude = unit.geo_location.Latitude,
                                longitude = unit.geo_location.Longitude
                          })
                        .ToList()
                       .Select(x => new object[] { // write all properties here like x.index, x.c_number });

创建一个包含所有值的类,例如:

public class MyClass {
  public int index { get; set; }
  public int c_number { get; set; }
  public string serial_number { get; set; }
  public int unused get; set; }
  public string ip_address get; set; }
  public string build_version { get; set; }
  public Status statuses { get; set; }
  public int index { get; set; }
  ...etc...
}

那么做:

var currUnitFaster = (from unit in pacificRepo.Units
                                 join loc in pacificRepo.GeoLocations on new { unit.geo_location.Latitude, unit.geo_location.Longitude } equals new { loc.geo_location.Latitude, loc.geo_location.Longitude }
                                 join st in pacificRepo.UnitStatuses on unit.c_number equals st.c_number
                                 select new MyClass { 
                                    index = 0, 
                                    c_number = unit.c_number, 
                                    serial_number = unit.serial_number, 
                                    ...etc...
                                 }).ToList();

问题出在

select new { 

部分。您应该定义您想要的类型(并且它应该适合您返回的数据),将其更改为诸如

之类的内容。
select new MyExpectedReturnType {