Linq to sql 不支持到SQL的转换

Linq to sql 不支持到SQL的转换,linq-to-sql,Linq To Sql,我们有以下代码: private IList<InfoRequest> GetBy(Func<InformationRequest, string> func, string searchby) { var requests = _dc.InformationRequests .Where(x => func.Invoke(x).Contains(searchby)) .

我们有以下代码:

private IList<InfoRequest> GetBy(Func<InformationRequest, string> func, string searchby)
{
    var requests = _dc.InformationRequests
                      .Where(x => func.Invoke(x).Contains(searchby))
                      .OrderBy(y => y.RequestDate);

    return Mapper.Map<InformationRequest[], InfoRequest[]>(requests.ToArray());
}

它继续抛出“不支持转换为SQL”错误。关于这个问题或如何解决这个问题有什么想法吗?

我会采纳本文中的建议,将func转换为表达式,这将导致不同的重载。调用Where方法

 private IList<InfoRequest> GetBy(Expression<Func<InformationRequest, string>> exp, string searchby)
 {

      var requests = _dc.InformationRequests
                  .Where(x => exp(x).Contains(searchby))
我的结局是:

 private static Expression<Func<T, bool>> StartsWith<T>(Func<string, string> func)
{
    var searchBy = func.Method.GetParameters()[0].Name;
    var search = Expression.Constant(func(null), typeof(string));

    var searchByParam = Expression.Parameter(typeof(T), searchBy);
    var searchByExp = Expression.Property(searchByParam, searchBy);

    var methodInfo = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });//, typeof(StringComparison)});
    var containsExpression = Expression.Call(searchByExp, methodInfo, search);

    return Expression.Lambda<Func<T, bool>>(containsExpression, searchByParam);
}

如果您想了解更多详细信息,我在这里写了一篇博客:

支持Contains,但不支持func。提供有关您试图使用Func执行的操作的更多信息。这是你的问题。Linq to SQL不能将表达式下推到db。是的,我也试过了。它没有给我传递信息请求的选项。我必须这样做:.Wherex=>exp.Compile.Invokex.Containssearchby在这一点上,它给出了与以前相同的错误。您能在上面发布您的函数吗?