如何在 Linq to Entity select 中使用 func?

How do I use func in Linq to Entity select?

我想要像下面这样的用法,但是如果我不使用 AsEnumerable 就会报错。 为什么我想要它,因为我将在很多地方需要它。

还有其他方法吗?

public static class EntityHelper
{
    public static Func<TABLE1, TABLE2, TABLE2, string> SelectTitle = (trn, acc, parentAcc) => trn.master_id != trn.parent_id ? parentAcc.name : acc.name;
}
 var query = from trn in context.TABLE1
      join acc in context.TABLE2 on trn.acc_id equals acc.id
      join parentAcc in context.TABLE2 on trn.parent_id equals parentAcc.id                            
      select new {name = EntityHelper.SelectTitle.Invoke(trn,acc,parentAcc)};

System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.String Invoke(TABLE1, TABLE2,TABLE2)' method, and this method cannot be translated into a store expression.'

我使用 LinqKit nuget 解决了这项工作。 我更改了我的代码如下。

public static class EntityHelper
{
     public static Expression<Func<TABLE1, TABLE2, TABLE2, string>> SelectTitle = (trn, acc, parentAcc) => trn.master_id != trn.parent_id ? parentAcc.name : acc.name;
}
 var query = from trn in context.TABLE1.AsExpandableEF()
      join acc in context.TABLE2 on trn.acc_id equals acc.id
      join parentAcc in context.TABLE2 on trn.parent_id equals parentAcc.id                            
      select new {name = EntityHelper.SelectTitle.Invoke(trn,acc,parentAcc)};