【发布时间】: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<T>) 上的 LINQ 可以处理任何有效的 C# 表达式,因为 lambda 由 C# 编译器编译,并且只需调用委托即可调用。其他 LINQ 提供程序(例如,在IQueryable<T>上运行)必须将您的 lambda 转换为其他内容(例如,SQL 查询)。由于并非所有有效的 C# 表达式都可以转换为其他内容(SQL 与 C# 相比非常有限),因此此类提供程序只能处理非常特定类型的表达式。因此,您发现了您的提供程序无法处理的表达式。
标签: c# asp.net linq entity-framework