How to get byte array length using LINQ to Entities?

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

I tried the following:

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

The problem is that I get the following exception:

In LINQ to Entities, the LINQ node type expression 'ArrayLength' is not supported.

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

+7
arrays c # linq linq-to-entities
source share
2 answers

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

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

See: Linq2EF trap: using the Length property raises a System.NotSupportedException

+22
source share

Your LINQ to Entities query is converted to SQL, if possible. Since the translator does not display Array.Length for 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 request this field and it will work.

+3
source share

All Articles