"Only arguments that can be evaluated on the client for the String.Contains method are supported."

public static void MyFunction(MyErrorClass err)
{
    var query = from filter in DataContext.ErrorFilters select filter;
    query = query.Where(f => err.ErrorMessage.Contains(f.ErrorMessage));
    List<ErrorFilter> filters = query.ToList();
    //...more code
}

So, I have some problems with the above code, and I get an error message from the subject line in the line query.ToList(). Here is what I am trying to do:

Firstly, I have my own kind of errors MyErrorClass. Whenever an error occurs on my site, I create an object MyErrorClassfrom the exception, save all the data from the exception in this object and save the information in the database.

One of the properties of the exception I'm tracking is the error message ( ErrorMessage). I have a table ErrorFiltersconfigured in a database where the user can filter errors based on ErrorMessage. Therefore, say that you get a lot of errors that say: "System.Data.SqlClient.SqlException: timed out. The timeout period expires before the operation completes or the server does not respond." And you want to ignore them. You simply add a filter to the database with ErrorMessage as "timeout expired" and set it to ignore.

Now my class above is set to accept the error and decides whether to filter the error. I am trying to get a list of all filters that have an ErrorMessageerror.

I am sure this is a simple solution, I just don’t know how to fix it.

+6
2

, f.ErrorMessage.Contains(err.ErrorMessage) - linq to sql WHERE ErrorFilter.ErrorMessage LIKE %err.ErrorMessage%. , , , SQL where , , .

, var query = from filter in DataContext.ErrorFilters select filter; , :

var filters = DataContext.ErrorFilters.Where(f => f.ErrorMessage.Contains(err.ErrorMessage)).ToList();

EDIT:

. , , , linq2sql. datacontext ErrorFilter:

create procedure GetMatchingFilters @message varchar(500)
as
begin
    select  *
    from ErrorFilter
    where @message LIKE '%'+ErrorMessage+'%'
end

datacontext :

DataContext
    .GetMatchingFilters(err.ErrorMessage)
    .Select(result => new ErrorFilter {...})
    .ToList();
0

... , Linq2SQL IndexOf , Contains. :

public static void MyFunction(MyErrorClass err)
{
    var query = DataContext.ErrorFilters;
    query = query.Where(f => err.ErrorMessage.IndexOf(f.ErrorMessage)>=0);
    List<ErrorFilter> filters = query.ToList();
    //...more code
}

LinqPad , CHARINDEX, , "", " ", .

+7

All Articles