查询返回匿名类型列表,而不是列表值

gny*_*his 1 .net c# linq asp.net

我正在使用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();
Run Code Online (Sandbox Code Playgroud)

因此,列表中返回的每个元素都是匿名类型:

        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 }"
Run Code Online (Sandbox Code Playgroud)

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

currUnitFaster.First()[0] ==> "0"
currUnitFaster.First()[1] ==> "J0000014"
currUnitFaster.First()[2] ==> "A2F0JA02"
...
Run Code Online (Sandbox Code Playgroud)

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


编辑:我已经采取了塞尔曼的建议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."}
Run Code Online (Sandbox Code Playgroud)

Sel*_*enç 5

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

编辑:似乎EF抱怨casts.Try this:

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 });
Run Code Online (Sandbox Code Playgroud)