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

血红的双手。 提交于 2019-12-18 07:39:50

问题


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?


回答1:


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

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

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




回答2:


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.



来源:https://stackoverflow.com/questions/19496301/how-to-get-a-byte-array-length-using-linq-to-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!