Entity framework and VARBINARY

前端 未结 3 474
小蘑菇
小蘑菇 2021-01-12 14:42

I’m using the .NET entity framework and I’ve got one entity containing a varbinary. Is there an easy way to get the size of the varbinary in the codebehind, efter it’s been

3条回答
  •  暖寄归人
    2021-01-12 15:03

    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();
    

提交回复
热议问题