【问题标题】:LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)'LINQ to Entities 无法识别方法“Int32 ToInt32(Boolean)”
【发布时间】:2015-05-03 14:37:48
【问题描述】:

所以基本上,我试图将一组值返回给 GridView,它不满足数组中的任何值。但是,在尝试时,我收到了

的错误

LINQ to Entities 无法识别方法“Int32 ToInt32(Boolean)”方法,并且该方法无法转换为存储表达式。

这是我的代码:

        public List<Room> getAvailRoom()
    {
        //Sessions from Default Page
        DateTime checkedIn = Convert.ToDateTime(System.Web.HttpContext.Current.Session["checkIn"]);
        DateTime checkedOut = Convert.ToDateTime(System.Web.HttpContext.Current.Session["checkOut"]);

        //retrieves all the bookings which happen between two dates
        var booking = (from b in context.Booking
                         where b.departureDate >= checkedIn && b.arrivalDate <= checkedOut
                         select b);

        //Counts how many rooms are booked during those dates
        int countRooms = booking.Count();

        int[] bookings = new int[countRooms];
        foreach (var booktypes in booking)
        {

                for (int i = 0; i < countRooms; i++)
                {
                    //Addings the RoomIds to the array
                    bookings[i] = booktypes.RoomId;
                }

        }
        //Returns values that does not equal to any roomIds within the bookings array.
        return (from b in context.Room
                where b.roomId != Convert.ToInt32(bookings.Any())
                select b).ToList();
    }

任何想法我做错了什么?

【问题讨论】:

  • 值得解释为什么会发生这种情况。内存对象 (IEnumerable&lt;T&gt;) 上的 LINQ 可以处理任何有效的 C# 表达式,因为 lambda 由 C# 编译器编译,并且只需调用委托即可调用。其他 LINQ 提供程序(例如,在 IQueryable&lt;T&gt; 上运行)必须将您的 lambda 转换为其他内容(例如,SQL 查询)。由于并非所有有效的 C# 表达式都可以转换为其他内容(SQL 与 C# 相比非常有限),因此此类提供程序只能处理非常特定类型的表达式。因此,您发现了您的提供程序无法处理的表达式。

标签: c# asp.net linq entity-framework


【解决方案1】:

我认为您确实在寻找Contains 查询:

 return (from b in context.Room
                where !bookings.Contains(b.roomId) 
                select b).ToList();

你的 sn-p 有两个问题:

  • Any() 返回一个布尔值 - 这根本不是你想要的
  • 您正在尝试在 SQL 领域中执行 Convert.ToInt32 - 没有 相当于那里(基本上 Linq to Entities 无法将此部分转换为 SQL 查询),所以这不起作用。

【讨论】:

    【解决方案2】:

    注意:“bookings.Any()”将返回一个 true 或 false 指示预订数组中是否有任何元素。您收到此错误是因为您尝试将表达式“bookings.Any()”的结果的布尔值转换为整数。

    如果你想做的是让所有房间都没有预订,那么我会推荐

          if(bookings.Any()){
           return (from b in context.Room
                where !bookings.contains(b.roomId)  
                select b).ToList();
           }else{
    
           return (from b in context.Room  
                select b).ToList();
          }
    

    含义:如果我们预订了任何房间,则选择那些没有预订的房间,否则选择所有房间(因为没有预订房间)

    注意:如果您需要进行类型转换,请在链接外部进行转换并在链接语句中使用您的结果。由于这是 C#,我认为您在此处的类型转换没有任何问题。 你的“bookings[i] = booktypes.RoomId;”应该返回一个不需要转换的 int 数组,即“Convert.ToInt32”,但如果需要进行此转换,请在您之前进行,即

        if(bookings.Any()){
           var thebooking = new List<int>();
         foreach (var booking in bookings)
        {
           thebooking.Add(Convert.ToInt32(booking));
         }
    
           return (from b in context.Room
                where !thebooking.contains(b.roomId)  
                select b).ToList();
           }else{
    
           return (from b in context.Room  
                select b).ToList();
          }
    

    【讨论】:

    • 看不懂Convert.ToInt32(bookings.Any())的意思。这是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多