Entity framework and VARBINARY Entity framework and VARBINARY asp.net asp.net

Entity framework and VARBINARY


A varbinary translates to a byte[] field in Entity Framework, which means you can check the Length property of the array:

int fieldSize = entity.MyVarBinaryField.Length;

As mentioned by tster: In a LINQ to Entities query, you can call the DataLength method of the SqlFunctions class, which will translate into a DATALENGTH function call in the generated SQL statement. This only works with SQL Server and Entity Framework 4 or later:

int? fieldSize = repository.Entity  .Select(e => SqlFunctions.DataLength(e.MyVarBinaryField)).Single();


I know this question is old, but EF now supports this by using SqlFunctions.DataLength()


I solved it by running another query, getting the DATALENGTH() of the cell. Not the smoothest way but it works.

I'm still interested in hearing if anyone can come up with an answer for this though.