重用lambda表达式
本文关键字:表达式 lambda 重用 | 更新日期: 2023-09-27 18:07:02
今天我有这个重复的代码:
public class MyProjectionExpressions
{
public static Expression<Func<Log, dynamic>> LogSelector()
{
return log => new
{
logId = log.LogId,
message = log.Message,
};
}
public static dynamic LogSelector(Log log)
{
return new
{
logId = log.LogId,
message = log.Message,
};
}
}
第一个方法是一个可重用的投影表达式,我这样使用它:
db.Logs.Select(MyProjectionExpressions.LogSelector());
第二个方法在不处理集合,而只处理一个需要序列化的项时使用,如下所示:
MyProjectionExpressions.LogSelector(someLog);
我尝试重用代码,像这样:
public class MyProjectionExpressions
{
public static Expression<Func<Log, dynamic>> LogSelector()
{
return log => MyProjectionExpressions.LogSelector(log);
}
public static dynamic LogSelector(Log log)
{
return new
{
logId = log.LogId,
message = log.Message,
};
}
}
但是,我遇到了"链接到实体vs链接到对象"的问题,也就是说:我必须像这样重用我的投影表达式:
db.Logs.ToList().Select(MyProjectionExpressions.LogSelector());
因为MyProjectionExpressions.LogSelector()
现在包含(使用)一个Linq-To-Entities无法识别的方法:
LINQ to Entities does not recognize the method 'MyProjectionExpressions.LogSelector()'
问题是,最后:
我如何重用lamba表达式的方式,链接到实体继续工作?
这个lambda表达式:
log => new
{
logId = log.LogId,
message = log.Message,
};
你可以这样做:
public class MyProjectionExpressions
{
public static Expression<Func<Log, dynamic>> LogSelector()
{
return log => new
{
logId = log.LogId,
message = log.Message,
};
}
//Get the expression as a Func<Log, dynamic>, then apply it
//to your log object.
private static Func<Log, dynamic> impl = LogSelector().Compile();
public static dynamic LogSelector(Log log)
{
return impl(log);
}
}
不是根据编译的方法来实现表达式,而是根据表达式来实现编译的方法:
public class MyProjectionExpressions
{
public static Expression<Func<Log, dynamic>> LogSelector()
{
return log => new
{
logId = log.LogId,
message = log.Message,
};
}
private static Lazy<Func<Log, dynamic>> func;
static MyProjectionExpressions()
{
func = new Lazy<Func<Log, dynamic>>(() => LogSelector().Compile());
}
public static dynamic LogSelector(Log log)
{
return func.Value(log);
}
}
请注意,为了避免每次需要使用表达式时都不断地重新编译表达式,您可以缓存编译后的表达式并重新使用它。