VARBINARY and the Entity Framework

asp.net c# entity-framework sql-server-2005

Question

I have one entity that contains a binary and I'm using the.NET entity framework. If a variable has been obtained from the database, is there a simple method to determine its size in the code behind?

I'm wondering whether there is a method to get the size straight from the entity, similar to entity. Context.Size — or is there another way to manage it?

1
7
3/3/2009 9:52:14 AM

Accepted Answer

A varbinary is equivalent to abyte[] You may examine the array's Length attribute by using the field in Entity Framework:

int fieldSize = entity.MyVarBinaryField.Length;

As stated by tster: You may use the DataLength method of the SqlFunctions class in a LINQ to Entities query, which will convert into a call to the DATALENGTH function in the produced SQL statement. Entity Framework 4 or later and SQL Server are required for this to function:

int? fieldSize = repository.Entity
  .Select(e => SqlFunctions.DataLength(e.MyVarBinaryField)).Single();
19
5/31/2018 12:32:13 PM

Popular Answer

Although I realize this is an old query, EF now supports this by usingSqlFunctions.DataLength()



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow