Just trying to do something simple like this:
context.Users.Any(fun currentUser -> currentUser.UserName = userName)
Where Context is just an entity framework context. Now when I hover over “currentUser” it knows that it is a User type. However I get the:
Lookup on object of indeterminate type based on information prior to
this program point. A type annotation may be needed prior to this
program point to constrain the type of the object. This may allow the
lookup to be resolved.
Now I realize that I can do this:
context.Users.Any(fun (currentUser:User) -> currentUser.UserName = userName)
But that seems really silly since c# can easily infer type with:
context.Users.Any(currentUser => currentUser.UserName = userName)
Full method is this:
let FindAndRemoveUser(userName:String, context:StoryBoardContext) =
if context.Users.Any(fun currentUser-> currentUser.UserName = userName) then
let foundUser = context.Users.Where(fun innerUser -> innerUser.UserName = userName).First()
context.Users.DeleteObject(foundUser)
context.SaveAll() |> ignore
Am I wrong for thinking that F# should handle type inference as well or better than C#?
I think that your approach has a more fundamental issue than just the problem you described. When you use
WhereorAnywith a lambda expression in C#, the C# compiler turns the lambda into an expression treeExpression<Func<_, _>>and so LINQ to Entities can translate the code to an SQL query.However, when you use F# lambda function as an argument, it will be compiled as a function (or a delegate of type
Func<_, _>). This means that your code will call in-memory version of the processing function and you’ll do all processing in memory instead of doing it on the database server!To write a query in F# 2.0, you need to wrap all code inside a quotation and run it using
queryfunction from F# PowerPack (F# 3.0 is going to make this a lot nicer, but that’s unfortunately just a beta). You probably need something like this:(Aside, I’m not sure if you need to check whether a user exists upfront – you can just find all users using just
filterand then delete the first one if the returned sequence contains something)