如何返回强类型列表F#

时间:2018-01-05 18:33:05

标签: linq f#

我需要将数据库中的列表作为强类型返回

    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中仅支持无参数构造函数和初始值设定项。”

1 个答案:

答案 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