문제

Hey there! I'm trying to write a POCO class in proper F#... But something is wrong..

The C# code that I want to "translate" to proper F# is:

public class MyTest
{
    [Key]
    public int ID { get; set; }

    public string Name { get; set; }
}

The closest I can come to the above code in F# is something like:

type Mytest() =

    let mutable _id : int = 0;
    let mutable _name : string = null;

    [<KeyAttribute>]
    member x.ID
        with public get() : int = _id
        and  public set(value) = _id <- value

    member x.Name 
        with public get() : string = _name
        and  public set value = _name <- value

However when I try to access the properties of the F# version it just returns a compile error saying

"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."

The code thats trying to get the property is a part of my Repository (I'm using EF Code First).

module Databasethings =

    let GetEntries =
        let ctx = new SevenContext()
        let mydbset = ctx.Set<MyTest>()
        let entries = mydbset.Select(fun item -> item.Name).ToList() // This line comes up with a compile error at "item.Name" (the compile error is written above)
        entries

What the hell is going on?

Thanks in advance!

도움이 되었습니까?

해결책

Your class definition is fine, it's your LINQ that has a problem. The Select method is expecting an argument of type Expression<Func<MyTest,T>> but you're passing it a value of type FSharpFunc<MyTest,T> - or something similar to that anyway.

The point is you can't use F# lambda expressions directly with LINQ. You need to write your expression as an F# Quotation and then use the F# PowerPack to run the code against an IQueryable<> data source. Don Syme has a good overview of how this works.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top