我有两个查询:
member private x.CheckIfUserExistsUsingId(userId:int, context:StoryBoardContext) =
<@ context.Users
|> Seq.exists (fun currentUser -> currentUser.Id = userId) @>
和
member private x.FindUserById(userId:int, context:StoryBoardContext) =
<@ context.Users
|> Seq.filter(fun currentUser -> currentUser.Id = userId)
|> Seq.head @>
我想重构它,使这两个
fun currentUser -> currentUser.Id = userId
可以是一个方法,如:
member private x.IfUserIdMatches (userId:int) =
fun (currentUser:User) -> currentUser.Id = userId
然后使用它:
member private x.CheckIfUserExistsUsingId(userId:int, context:StoryBoardContext) =
<@ context.Users
|> Seq.exists (x.IfUserIdMatches (userId)) @>
但是我一直得到一个错误:
The following construct was used in query but is not recognised by the F#-to-LINQ query translator...
这让我觉得我的方法签名构造得很差。作为f#的新手,这让我有点困惑,因为我确信这可以在c#中使用返回Func的方法来完成。然而,我理解这是有区别的,因为f#使用不同的库来构造linq查询。
如果您将方法更改为返回Expr
,它是否工作
member private x.IfUserIdMatches (userId:int) =
<@ fun (currentUser:User) -> currentUser.Id = userId @>
和使用拼接?
member private x.CheckIfUserExistsUsingId(userId:int, context:StoryBoardContext) =
<@ context.Users
|> Seq.exists %(x.IfUserIdMatches (userId)) @>