Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a byte array length using LINQ to Entities?

I have a Document class that stores the data of that document as a byte array. I need to check the size of the array, using LINQ to Entities.

I have tried the following:

[long Linq query here...] o.Data.Length < 800000)

The problem is I get the following exception:

The LINQ expression node type 'ArrayLength' is not supported in LINQ to Entities."

Is there any other way to check the size of the byte array?

like image 604
E.Vaughan Avatar asked Oct 21 '13 13:10

E.Vaughan


2 Answers

Use SqlFunctions.DataLength Method (Byte[]) to compare length.

yourquery..... SqlFunctions.DataLength(o.Data) < 800000)

See: Linq2EF pitfall: Using Length property causes System.NotSupportedException

like image 138
Habib Avatar answered Nov 16 '22 11:11

Habib


Your LINQ to Entities query is translated into SQL, provided this is possible. Since the translator does not map Array.Length to any SQL method, you cannot use it in LINQ to Entities queries.

What you can do is add a column to your table OR create a VIEW that calculates the length and exposes it as a column. Then you can query against that field and it will work.

like image 44
Roy Dictus Avatar answered Nov 16 '22 11:11

Roy Dictus