5

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.

1
  • @Axarydax - I wouldn't call that related. That answer properly tests length and that the same elements are in the array...but ignores sequence. In the OPs case, sequence matters as well. Commented Jan 19, 2011 at 19:38

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Oops, I didn't mean to have that there, it is actually LINQ to Objects. Thanks.
There are no array properties in LINQ-to-SQL (or Entity Framework) either, so that would be a non-issue.
Not true - VARBINARY maps to byte[].
Timestamp also maps to byte[].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.