gpt4 book ai didi

c# - LINQ to Entities 无法识别方法 'Int32 ToInt32(Boolean)'

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:47 28 4
gpt4 key购买 nike

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

的错误

LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)' method, and this method cannot be translated into a store expression.

这是我的代码:

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

知道我做错了什么吗?

最佳答案

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

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

您的代码段有两个问题:

  • Any() 返回一个 bool 值 - 这根本不是您想要的
  • 您正在尝试在 SQL 领域中执行 Convert.ToInt32 - 没有等同于此(基本上 Linq to Entities 无法将这部分转换为 SQL 查询),所以这行不通。

关于c# - LINQ to Entities 无法识别方法 'Int32 ToInt32(Boolean)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30015072/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com