4

嘿!我正在尝试用正确的 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

这到底是怎么回事?

提前致谢!

4

1 回答 1

7

您的类定义很好,是您的 LINQ 有问题。该Select方法需要一个类型的参数,Expression<Func<MyTest,T>>但您正在向它传递一个类型的值FSharpFunc<MyTest,T>- 或者类似的东西。

关键是您不能直接将 F# lambda 表达式与 LINQ 一起使用。您需要将表达式编写为F# 引用,然后使用F# PowerPack针对IQueryable<>数据源运行代码。Don Syme 对它的工作原理有一个很好的概述

于 2010-12-30T21:14:11.940 回答