[英]Returning Expression Lambda From Function Call Issue
I need to have this function below, where if the enum value is C it just places Boolean true in the second lambda value instead of retrieving it from the User properties.
public static Expression<Func<User, bool>> IsValid(this Component.ValueEnum component)
{
return component switch
{
Component.ValueEnum.A => u:User => u.token.x.IsValid,
Component.ValueEnum.B => u:User => u.token.y.IsValid,
Component.ValueEnum.C => u:User => true,
_ => throw new ArgumentException($"unrecognized component {component}")
};
}
但是,当我尝试这样做时,它会抛出异常:
System.InvalidOperationException:无法确定 u => True 的序列化信息
我究竟做错了什么? 可能吗?
更新 1:
好的,我看到这条线是不可能的:
Component.ValueEnum.C => u:User => true
生成的 Lambda 被用于过滤用户集合,这是非法操作。
为了简化,我删除了 Expression 作为返回类型。 如果您在里面只需要一个 lamda function 接受一个User
并直接或使用User
返回一个bool
,那么对代码进行以下更改可能会对您有所帮助 -
public static Func<User, bool> IsValid(this Component.ValueEnum component)
{
return component switch
{
Component.ValueEnum.A => (User u) => u.token.x.IsValid,
Component.ValueEnum.B => (User u) => u.token.y.IsValid,
Component.ValueEnum.C => (User u) => true,
_ => throw new ArgumentException($"unrecognized component {component}")
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.