Dapper-Extensions
Dapper-Extensions copied to clipboard
Predicates dont work with byte[]
Table:
CREATE TABLE [dbo].[TestBytes](
[Id] [int] IDENTITY(1,1) NOT NULL,
[KeyBytes] [varbinary](16) NOT NULL,
CONSTRAINT [PK_TestBytes] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
POCO:
class TestBytes
{
public int Id { get; set; }
public byte[] KeyBytes { get; set; }
}
Console test app:
static void Main(string[] args)
{
using(var db = new SqlConnection(@"..."))
{
db.Open();
var keyBytes1 = new byte[16];
keyBytes1[0] = 128;
var keyBytes2 = new byte[16];
keyBytes2[0] = 111;
var keyBytes3 = new byte[16];
keyBytes2[0] = 55;
var testBytes1 = new TestBytes() { KeyBytes = keyBytes1 };
var testBytes2 = new TestBytes() { KeyBytes = keyBytes2 };
db.Insert<TestBytes>(testBytes1);
db.Insert<TestBytes>(testBytes2);
var pg = new PredicateGroup() { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
pg.Predicates.Add(Predicates.Field<TestBytes>(t => t.KeyBytes, Operator.Eq, keyBytes3));
var matchedKeys = db.GetList<TestBytes>(pg);
Console.WriteLine(matchedKeys.Count()); // print 2 - fail
var mathcedKeys2 = db.Query<TestBytes>("Select * from TestBytes where KeyBytes=@KeyBytes", new { KeyBytes = keyBytes3 });
Console.WriteLine(mathcedKeys2.Count()); //print 0 - ok
mathcedKeys2 = db.Query<TestBytes>("Select * from TestBytes where KeyBytes=@KeyBytes", new { KeyBytes = keyBytes2 });
Console.WriteLine(mathcedKeys2.Count()); //print 1 - ok
db.Close();
}
Console.ReadLine();
}
If I pass keyBytes2 or 1 to predicate -> same result. But Dapper Query works as expected.