【问题标题】:Converting anonymous type to IList将匿名类型转换为 IList
【发布时间】:2012-01-20 22:06:26
【问题描述】:

我收到了一个我从以下代码中不太明白的错误:

public IList<Store> getNearbyStores(double x, double y)
{
    var result = (from T in
                      (
                        (from stores in dc.Stores
                           select new
                           {
                               stores.id,
                               stores.name,
                               stores.city,
                               stores.typeID,
                               stores.latitude,
                               stores.longitude,
                               stores.tag1,
                               stores.tag2,
                               Distance = (System.Double?)(6371 * Math.Acos((double)Math.Cos((double)(Math.PI * x) / 180) * Math.Cos((double)(Math.PI * stores.latitude) / 180) * Math.Cos((double)(Math.PI * stores.longitude) / 180 - (Math.PI * y) / 180) + Math.Sin((double)(Math.PI * x) / 180) * Math.Sin((double)(Math.PI * stores.latitude) / 180)))
                           }))
                  where
                    T.Distance < 5
                  orderby
                    T.Distance
                  select new
                  {
                      T.id,
                      T.name,
                      T.city,
                      T.typeID,
                      T.latitude,
                      T.longitude,
                      T.tag1,
                      T.tag2,
                      T.Distance
                  }).ToList();

    return result;
}

错误是:

Error   1   Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IList<Store>'. An explicit conversion exists (are you missing a cast?) C:\Users\Joris\Desktop\ShopperNET\App_Code\DAL\DALstore.cs  104 16  C:\...\ShopperNET\

如何将匿名返回类型转换为 IList?我认为 toList() 会修复它,但它没有..我尝试了一些我在网上找到的东西,比如只使用“列表”,但没有一个真正帮助我。

提前致谢。

【问题讨论】:

    标签: c# asp.net .net casting ilist


    【解决方案1】:

    我认为您应该使用 IntoLet 密钥,而不是执行第 2 步。

    查看帖子了解更多详情:Into and let in LINQ ( Let vs Into)

    例子

    from student in students
                    let x = student.Scores[0] + student.Scores[1] +
                        student.Scores[2] + student.Scores[3]
                    where x > averageScore
                    select new { id = student.ID, score = x };
    

    【讨论】:

      【解决方案2】:

      使用

          select new Store()
          {
            Id = stores.id,
            Name = stores.name,
            ... 
          }
      ...
      

      而不是select new { } 选择Store 类型的实例而不是匿名类型的实例

      (您可以替换这两个事件,但重要的是第二个,因为这是将返回的内容)

      类型初始化器见MSDN, MSDN

      【讨论】:

      • 当我这样做时我得到Error 1 Cannot initialize type 'Store' with a collection initializer because it does not implement 'System.Collections.IEnumerable'。新错误? ;(
      • 当我也更改第一个时,我得到了这个Error 1 Invalid initializer member declarator。这些错误是如此模糊。我在 .NET 中做的第一个项目,真的让我很烦:(
      • 好的;我想我通过你对ID = 所做的事情来解决它。但是,我有一个字段“距离”不在我的数据库中,所以也不在我的模型中。这有点像普通 SQL 中的AS Distance。我不能把它分配给任何东西,可以吗?它现在抱怨它:Error 1 'Store' does not contain a definition for 'Distance'。还在这里学习:(
      • 是的,您需要Distance 的属性或字段。您可以扩展您的 Store 类或考虑在您的方法中返回其他内容(例如,提供所有需要的属性的自定义类)
      【解决方案3】:

      您的方法签名期望返回 List&lt;Store&gt;,您可以尝试从您的 LINQ 查询中返回它,例如

      //top bit of query
      //.
      select new Store()
      {
          Id = T.id,
          Name = T.name,
          //etc..
      }).ToList();
      

      编辑:根据以下 Pranay 的建议,以下应该可行:

      var result = from store in dc.Stores
                   let Distance = (System.Double?)(6371 * Math.Acos((double)Math.Cos((double)(Math.PI * x) / 180) * Math.Cos((double)(Math.PI * store.latitude) / 180) * Math.Cos((double)(Math.PI * store.longitude) / 180 - (Math.PI * y) / 180) + Math.Sin((double)(Math.PI * x) / 180) * Math.Sin((double)(Math.PI * store.latitude) / 180)))
                   where Distance < 5
                   order by Distance
                   select store;
      
      return result.ToList();
      

      【讨论】:

      • 这会导致一个新错误:Error 1 Invalid initializer member declarator. 不知道这意味着什么,brb google。
      • 谢谢。这立即解决了我的问题。没想到那样做,还没有听说过 let thingie。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 2011-04-19
      相关资源
      最近更新 更多