ebb*_*ebb 4 f# poco ef-code-first
嘿!我正在尝试用适当的F#写一个POCO课......但是出了点问题......
我想"翻译"到正确的F#的C#代码是:
public class MyTest
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我最接近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
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试访问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
Run Code Online (Sandbox Code Playgroud)
这到底是怎么回事?
提前致谢!
你的类定义很好,这是你的LINQ有问题.该Select方法期望一个类型的参数,Expression<Func<MyTest,T>>但你传递的是一个类型的值FSharpFunc<MyTest,T>- 或者类似的东西.
关键是你不能直接用LINQ使用F#lambda表达式.您需要将表达式编写为F#Quotation,然后使用F#PowerPack针对IQueryable<>数据源运行代码.Don Syme 很好地概述了它的工作原理.
| 归档时间: |
|
| 查看次数: |
644 次 |
| 最近记录: |