F# - POCO类

时间:2010-12-30 20:21:05

标签: f# poco ef-code-first

嘿那里!我正在尝试用适当的F#写一个POCO课......但是出了点问题......

我想要“翻译”到正确的F#的C#代码是:

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

    public string Name { get; set; }
}

我最接近F#中的上述代码就像是:

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

但是当我尝试访问F#版本的属性时,它只返回一个编译错误,说

“基于此程序点之前的信息查找不确定类型的对象。在此程序点之前可能需要类型注释来约束对象的类型。这可以允许解析查找。 “

尝试获取属性的代码是我的存储库的一部分(我使用的是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

到底是怎么回事?

提前致谢!

1 个答案:

答案 0 :(得分:7)

你的类定义很好,这是你的LINQ有问题。 Select方法期望类型为Expression<Func<MyTest,T>>的参数,但您传递的值类型为FSharpFunc<MyTest,T> - 或类似于此类似的东西。

关键是你不能直接用LINQ使用F#lambda表达式。您需要将表达式编写为F# Quotation,然后使用F# PowerPack针对IQueryable<>数据源运行代码。 Don Syme有good overview of how this works

相关问题