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

后端 未结 2 1821
刺人心
刺人心 2021-01-18 04:56
public static void MyFunction(MyErrorClass err)
{
    var query = from filter in DataContext.ErrorFilters select filter;
    query = query.Where(f => err.ErrorMes         


        
2条回答
  •  情歌与酒
    2021-01-18 05:40

    It seems you should be using f.ErrorMessage.Contains(err.ErrorMessage) - linq to sql should then convert this to WHERE ErrorFilter.ErrorMessage LIKE %err.ErrorMessage%. The problem with the way you have it is that the generated SQL would need a dynamic string to match in the where clause, and hence could only be filtered on the client.

    Incidently, the var query = from filter in DataContext.ErrorFilters select filter; line is not required and you can just do:

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

    EDIT:

    Ok I see what you're trying to do now, but I'm not sure if this is possible in linq2sql. You could create a stored procedure and add that to your datacontext and do the mapping from the output to a sequence of ErrorFilter objects:

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

    Then in your datacontext you can do:

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

提交回复
热议问题