boolean Contains不支持转换为SQL

时间:2012-06-02 11:49:25

标签: sql linq linq-to-sql contains

我收到错误boolean Contains不支持转换为SQL

我有一些房间ID,应删除

我想删除此查询,但我无法成功。我怎么能这样做?

var rooms = from rooms in entity.Rooms 
            where myArray.contains(rooms.RoomID) select rooms;

3 个答案:

答案 0 :(得分:1)

您正在尝试混音。 Array.Contains发生在客户端上,但是你将它放在发送到sql server的表达式的中间。

您需要将整个批次发送到sql进行比较,或者从sql返回行并在本地进行比较。

rooms = from room in entity.Rooms.AsEnumerable()
        where myArray.contains(room.RoomID)
        select room;

或使用lambda语法:

rooms = entity.Rooms.AsEnumerable.Where(room => myArray.contains(room.RoomID));

答案 1 :(得分:0)

   var roomslist =myArray.ToList();
   var roomsTemp = entity.Rooms;
   var rooms=roomsTemp.Where(x=>roomslist.Contains(x.RoomId)).ToList();

如果这不起作用,那么这应该有用,但我不建议使用它:

   var roomslist =myArray.ToList();
   var roomsTemp = entity.Rooms.ToList();
   var rooms=roomsTemp.Where(x=>roomslist.Contains(x.RoomId)).ToList();

答案 2 :(得分:0)

自L2S以来已经有一段时间了,但是到了这里。我可以sworn int []。包含内置..你是偶然使用GUID吗?也许我在想EF。这有用吗:Linq query with Array in where clause?

如果您要为此编写SQL过程,则应传入ID的CSV字符串。使用相同的想法(警告我的头顶):

var ids = "," + string.Join(",",myArray) + ",";
var rooms = from rooms in entity.Rooms 
        where SqlMethods.Like("," + rooms.RoomID + ",", ids) select rooms;

也许ids.Contains("," + rooms.RoomID + ",")可以工作,因为它有一个转换为SQL内置(对于字符串)

Alternate是编写自己的UDF,将CSV分解为表并执行SELECT EXISTS。应该有很多这样的例子,因为它是你使用存储过程的方式。然后你可以:

var ids = "," + string.Join(",",myArray) + ",";
var rooms = from rooms in entity.Rooms 
        where MyMethods.Contains(ids,rooms.RoomID) select rooms;