This is how I made EF 7 build queries that compare byte[] values:
- Declared an empty method that accepts two byte arrays and returns bool in my context Class:
public partial class DbContext
{
public static bool LessThanOrEqual(byte[] a, byte[] b)
{
throw new NotImplementedException();
}
- In DbContetx.OnConfiguring() method defined a translation from this function to an SQL tree section:
modelBuilder.HasDbFunction(
typeof(DbContext).GetMethod(nameof(LessThanOrEqual), new[] { typeof(byte[]), typeof(byte[]) }))
.HasTranslation(
args =>
new SqlBinaryExpression(
ExpressionType.LessThanOrEqual,
args[0],
args[1],
args[0].Type,
args[0].TypeMapping)
);
- Used this method in my Linq query:
byte[] arr = ...
db.tbl.Where(r => DbContext.LessThanOrEqual(r.arr1, arr) &&
DbContext.LessThanOrEqual(arr, r.arr2))
- This Linq query produces the following SQL query:
SELECT ...
FROM tbl
WHERE (b.arr1 <= @__arr_0) AND (@__arr_0 <= b.arr2)