.NET实体框架 - 使用。载()找到一个地方前pression一个字节值 [英] .NET Entity Framework - Using .Contains() to find a byte value in a Where expression
问题描述
我建立的基础上,我从用户获取参数的IQueryable的。其中一个参数是多选,我需要检索包含任何选定值的记录。
I am building an IQueryable based on parameters I get from the user. One of those parameters is a multi-select and I need to retrieve records that contain any of the selected values.
在code表示与处理是:
The code that deals with that is:
VAR的id = parameters.DeliveryID.ToArray();
课程= courses.Where(C => ids.Contains(c.CourseDeliveryID));
var ids = parameters.DeliveryID.ToArray();
courses = courses.Where(c => ids.Contains(c.CourseDeliveryID));
在上面的code:
1. IDS - 是一个字节数组,我要确保它有多个值在调用包含()
。
2. c.CourseDeliveryID - 这是一个字节的值
In the above code:
1. ids - is a byte array and I make sure it has multiple values before calling Contains().
2. c.CourseDeliveryID - that's a byte value.
在数据库中,我存储CourseDeliveryID为TINYINT(SQL Server 2008中)。
In the database I store CourseDeliveryID as tinyint (SQL Server 2008).
编译就好了。
当我运行code,我得到以下的ArgumentException:
DbEx pressionBinding需要输入EX pression与集合与resultType。
参数名:输入
When I run the code I get the following ArgumentException:
DbExpressionBinding requires an input expression with a collection ResultType.
Parameter name: input
我发现了异常这里的文档: <一href="http://technet.microsoft.com/en-us/library/system.data.common.commandtrees.ex$p$pssionbuilder.dbex$p$pssionbuilder.bindas.aspx">http://technet.microsoft.com/en-us/library/system.data.common.commandtrees.ex$p$pssionbuilder.dbex$p$pssionbuilder.bindas.aspx
I found the documentation for that exception here: http://technet.microsoft.com/en-us/library/system.data.common.commandtrees.expressionbuilder.dbexpressionbuilder.bindas.aspx
在试图解决我发现,如果我用同样的code短裤,整数或多头,我没有任何问题的问题。
While trying to solve the problem I found that if I use the same code on shorts, ints or longs I don't have any problem.
我在昨天以来与微软触摸它,并会更新,当我知道更多,但同时,我想我会扔也在这里得到,如果可能的更多的建议。
I'm in touch with Microsoft about it since yesterday and will update when I know more, but in the meantime I figured I'd throw it also here to get more advises if possible.
在此先感谢!
推荐答案
我能够重现你的错误在LINQPad,发现使用名单,其中,字节&GT;
而不是的字节[]
将工作:
I was able to reproduce your error in LINQPad, and found that using a List<byte>
instead of a byte[]
would work:
// byte[] ids = new byte[] { 1, 64 }; <== causes ArgumentException
List<byte> ids = new List<byte> { 1, 64};
var c = Courses.Where (co => ids.Contains(co.CourseDeliveryId));
将生成的SQL语句,返回结果:
will generate the following sql and return results:
SELECT
[Extent1].[CourseId] AS [CourseId],
[Extent1].[CourseName] AS [CourseName],
[Extent1].[CourseDeliveryId] AS [CourseDeliveryId]
FROM [dbo].[Courses] AS [Extent1]
WHERE [Extent1].[CourseDeliveryId] IN (1,64)
这也是有趣的是,使用 INT []
或短[]
也将工作,生产这种SQL:
It's also interesting that using an int[]
or short[]
would also work, producing this sql:
SELECT
[Extent1].[CourseId] AS [CourseId],
[Extent1].[CourseName] AS [CourseName],
[Extent1].[CourseDeliveryId] AS [CourseDeliveryId]
FROM [dbo].[Courses] AS [Extent1]
WHERE (1 = CAST( [Extent1].[CourseDeliveryId] AS int)) OR (64 = CAST( [Extent1].[CourseDeliveryId] AS int))
但使用字节[]
导致异常。我只能猜测,在SQL Server EF提供商正试图把字节[]
在一些特殊的方式,造成这种异常。
but using a byte[]
causes an exception. I can only guess that the SQL Server EF provider is trying to treat byte[]
in some special way, resulting in this exception.
这篇关于.NET实体框架 - 使用。载()找到一个地方前pression一个字节值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!