byte attribute [] entity contains Linq string

1

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);

}

    
asked by fgc 09.11.2017 в 18:47
source

1 answer

1

When you do:

var p = from t in ... 
    select t;

... p is a IQueryable<T> , which means that, even after assigning the value to p , you are still in arming mode SQL query. That is, any .Where or .Select that you add later, will try to convert the expression in SQL.

But since the conditions that you try to add later are not translatable to SQL, nor do I believe that you intend to do this, the solution is to indicate in code where to stop the translation to SQL. That can be achieved by calling AsEnumerable() so that p is no longer a IQueryable<T> , but a IEnumerable<T> .

var p = (from t in ... 
    select t).AsEnumerable();

By doing this, all of the conditions that you add afterwards will be applied in memory to the data that returns the query to the database, instead of trying to incorporate these conditions into the SQL directly.

    
answered by 09.11.2017 / 19:26
source