type private EntityConnection =SqlEntityConnection<ConnectionString="Server=.;Initial Catalog=db;user id=sa;password=pass;Integrated Security=SSPI;MultipleActiveResultSets=true",Pluralize = true>
type CustomerName(firstName, middleInitial, lastName) =
member this.FirstName = firstName
member this.MiddleInitial = middleInitial
member this.LastName = lastName
let context=EntityConnection.GetDataContext()
// GET /api/values
member x.Get() =
query { for g in context.tblTest do
select (new CustomerName(g.firstName, g.middleInitial, g.lastName))
}
|> Seq.toList
返回错误消息
“LINQ to Entities中仅支持无参数构造函数和初始值设定项。”
答案 0 :(得分:3)
将query
中的字段作为元组返回,然后使用Seq.map
构建您的CustomerName
对象。
member x.Get() =
query { for g in context.tblTest do
select (g.firstName, g.middleInitial, g.lastName)
}
|> Seq.map (fun (firstName,middleInitial,lastName) -> new CustomerName(firstName, middleInitial, lastName))
|> Seq.toList