【问题标题】:LINQ to Entities does not recognize the method 'System.String ToString()' methodLINQ to Entities 无法识别方法“System.String ToString()”方法
【发布时间】:2010-11-08 07:01:51
【问题描述】:
string[] userIds = userList.Split(','); // is an array of integers
IList<User> users = (from user in this.repository.Users
                     where userIds.Contains(user.Id.ToString())
                     select user).ToList();

上面的查询给出了

System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

我能做什么?

【问题讨论】:

    标签: linq-to-entities


    【解决方案1】:

    use可以使用这样的东西,

    where userIds.Contains(SqlFunctions.StringConvert((double)user.Id))
    

    而不是where userIds.Contains(user.Id.ToString())

    这应该可以工作

    【讨论】:

      【解决方案2】:

      避免拨打ToString。你想要这样的东西:

      userIds.Contains(user.Id)
      

      要完成这项工作,列表userIds 必须是user.Id 具有的类型的集合。如果您想要整数,请使用int.Parse 将字符串转换为整数:

      int[] userIds = userList.Split(',').Select(s => int.Parse(s)).ToArray();
      

      【讨论】:

        猜你喜欢
        • 2017-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-25
        • 2014-12-26
        • 2013-07-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多