Tony
Tony

Reputation: 12695

Entity Framework 'ArrayIndex' is not supported in LINQ to Entities

in my SQL Server DB table, I have the binary column type.

I can't execute the query, why ?

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

I get the The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities error

Upvotes: 5

Views: 1265

Answers (3)

haim770
haim770

Reputation: 49095

If you're willing to change your query a bit, this would work:

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

See MSDN

Upvotes: 1

xanatos
xanatos

Reputation: 111860

In TSQL the SUBSTRING function can be used on binary/varbinary.

Somewhere define:

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

then

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

Upvotes: 3

Jon Koivula
Jon Koivula

Reputation: 947

These expressions are translated to SQL queries, and this is one of the things you cant do. Take a look here for more detail: LINQ To Entities doesn't recognize array index

Upvotes: 4

Related Questions