0

I have code like this:

context.myTable.Where(x => x.myByteArray.Contains(myString)).Load();

It's not working because byte array does not have "Contains" definition. So I want to do something like this:

context.myTable.Where(x => Encoding.Default.GetString(x.myByteArray).Contains(myString));

This also didn't work because of exception:

LINQ to Entities does not recognize the method 'System.String GetString(Byte[])' method, and this method cannot be translated into a store expression.

How could I search string in my byte array then using Linq-to-Entities?

6
  • I'm not sure you can actually do that. You may try to put your myByteArray into a list : x.myByteArray.ToList().Contains(myString). But the risk of that is that you will load all you database... eitherway you can't compare those two because bytes are not string... Commented Jul 31, 2017 at 12:44
  • SqlFunctions.CharIndex. Commented Jul 31, 2017 at 12:46
  • 1
    You should have a different SQL column type that actually supports text (like varchar(max)). Try to build a sql equivalent query of what you try to do with EF... if you can't do it, then EF will probably fail too. Commented Jul 31, 2017 at 12:53
  • 1
    Why do you have a Byte Array (VARBINARY ?) that apparently contains text? Commented Jul 31, 2017 at 13:08
  • I have Byte Array because this contains multiple types such as string, image, date, etc Commented Aug 1, 2017 at 7:38

1 Answer 1

1

Let me try to explain why this happens to you. Inside the where function, you've entered linq which doesn't have all the C# functions available you might need. What you're writing there is a query, as is stated on this page

What you might be looking for is the following:

String myString = "someValue";
byte[] myByteArray= Encoding.ASCII.GetBytes(myString);
var myEntity = context.myTable.Where(x => Arrays.equals(x.StringValue, myByteArray));

What this does is the following:

  • It creates your string

  • It converts your string into a byte array

  • It checks whether the value of the myByteArray is the same as the byte array of the entity object, and fills "myEntity" with the entity containing the byte array. If multiple matching entities are found, the var will become a List<T> of your entity type.

This would probably solve your issue.

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

2 Comments

You cannot compare byte[] with ==. And even if you could, that is not the same as Contains().
Edited answer based on the == statement. @HenkHolterman i think what he wanted to do was compare the two, not see if the string contains the other. Which is a mistake in his own provided code. At least, that's what his question sounds like.

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.