实体框架' ArrayIndex' LINQ to Entities不支持

时间:2015-05-11 17:42:00

标签: c# sql-server linq entity-framework

在我的SQL Server数据库表中,我有binary列类型。

我无法执行查询,为什么?

var data = (from x in model.MyTable
            where x.BinaryColumn[0] == 1
            select x).FirstOrDefault();

我收到The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities错误

3 个答案:

答案 0 :(得分:4)

这些表达式被转换为SQL查询,这是您无法做到的事情之一。请查看此处了解更多详情:LINQ To Entities doesn't recognize array index

答案 1 :(得分:3)

在TSQL中,SUBSTRING功能可用于binary / varbinary

某处定义:

[DbFunction("SqlServer", "SUBSTRING")]
public static byte[] SubString(byte[] field, int start, int length)
{
    throw new NotSupportedException("Direct calls are not supported.");
}

然后

var data = (from x in model.MyTable
            where Substring(x.BinaryColumn, 1, 1) == new byte[] { 1 }
            select x).FirstOrDefault();

答案 2 :(得分:1)

如果您愿意稍微更改一下您的查询,这将有效:

var data = (from x in model.MyTable
            where SqlFunctions.CharIndex(new byte[] { 1 }, x.BinaryColumn) == 1
            select x).FirstOrDefault();

请参阅MSDN