Good friends,
I am new to Entity and Linq and I have a problem that I can not solve. In a query I have to check if a byte [] attribute contains a string that they pass to me.
I have tried a thousand ways and it does not work, and finally using my own method but when I include the call to the method in the Linq query, this exception throws me saying that I can not include anything in Linq that can not be converted to SQL.
This is what I was trying, once I make the select with its conditions, I ask if the Content parameter that is the string to look for has something, if so, I call the CheckPatternIn array metohodo passing it the x.Document that is the byte [] where you have to search and the text to look for Content, converted to byte [] as ContentBytes, and skip the exception that I comment.
I have also tried to pass the document to string and compare it with the string that pass me by parameter but Linq gives me an exception for the same reason, although it is not a proper method, it does not allow it either.
using (GPC container = new GPC()) {
var p = from t in ...
select t;
if (Content != null) {
byte[] ContentBytes = System.Text.Encoding.ASCII.GetBytes(Content);
p = p.Where(x => this.CheckPatternInArray(x.Document, ContentBytes) == true);
}
var res = p.Select(t=> new Views.xxx()
{
IDTx = t.Ix,
....
});
res.OrderBy(o => o.Ix);
return res.Distinct().ToList();
private bool CheckPatternInArray(byte[] array, byte[] pattern) {
int fidx = 0;
int result = Array.FindIndex(array, 0, array.Length, (byte b) => {
fidx = (b == pattern[fidx]) ? fidx + 1 : 0;
return (fidx == pattern.Length);
});
return (result >= pattern.Length - 1);
}