Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Byte Arrays in LINQ to Objects

In a LINQ query:

from c in results
where c.ByteField == byteData
select c;

I'm getting no results from this even though the bytes are the same:

byte[5] = 49, 50, 51, 52, 53

How do I compare bytes properly in LINQ to Objects?

Thanks.

like image 723
Brian Mains Avatar asked Dec 16 '22 18:12

Brian Mains


1 Answers

In LINQ to Objects (as your post suggests in the title), you can use IEnumerable.SequenceEqual():

from c in results
where c.ByteField.SequenceEqual(byteData)
select c;

Unfortunately, it looks like you're using LINQ to SQL (or Entity Framework) based on your use of context. There's no SQL equivalent of SequenceEqual so this won't work in that case.

like image 104
Justin Niessner Avatar answered Jan 09 '23 18:01

Justin Niessner