【问题标题】:query is returning list of anonymous types, want list of values instead查询返回匿名类型列表,需要值列表
【发布时间】:2014-05-08 19:48:57
【问题描述】:

我正在使用 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 中的每个元素并手动将其添加到该列表中。我想知道是否有一些更清洁的转换或基于铸造的方法来做到这一点。


编辑:我已采纳 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."}

【问题讨论】:

    标签: c# asp.net .net linq


    【解决方案1】:

    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 });
    

    【讨论】:

    • 感谢您的提示!我做了以下操作,但现在收到了Unable to cast the type 'system.Nullable1...to type System.Object ...是否还需要更改其他内容?
    • 这不起作用,抛出我在编辑问题中给出的异常
    • @gnychis 看到我的更新,首先创建一个匿名类型列表,然后执行另一个投影。如果需要,您还可以在第二次选择后使用 ToList,然后您可以使用以下索引访问您的值:@987654325 @、currUnitFaster[0][1]
    【解决方案2】:

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

    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();
    

    【讨论】:

    • 根据问题的确切措辞,这应该被选作答案。这正是我要建议的。说真的,谁投反对票,为什么投反对票?请解释一下。
    • 我没有投反对票,但我也没有投赞成票。那是因为您建议的解决方案:1)要求某人仅为单个查询创建一个新类,并且 2)实际上并未提供所请求的解决方案?似乎您的最终结果将再次成为一组匿名类型
    • 这是你的假设,它是针对单个查询的,因为他没有指定他使用它的频率。仅仅因为为单个查询创建一个类并不是一个坏主意,它实际上使代码更清晰,并且将来更容易理解。这确实解决了他的问题。他担心匿名类型。来自问题:What is the easiest way to do this? I know that I can simply create a list, and then iterate through each element in currUnitFaster and manually add it to that list. **I was wondering if there is some cleaner transformation or casting-based way to do this.**
    • 第二点,除非我遗漏了什么,否则我的方法最终会得到 List 类型的 var .. 这是一个匿名类型数组吗?
    【解决方案3】:

    问题出在

    select new { 
    

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

    select new MyExpectedReturnType { 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      相关资源
      最近更新 更多