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

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.

于 2010-12-30T21:14:11.940 に答える