简体   繁体   English

查询结果到F#中的新表对象

[英]Query results to new table object in F#

Short question. 简短的问题。

I have just recently performed my first successful query in F#. 我最近刚刚在F#中执行了我的第一个成功查询。 I now want to be able to refer to the results and perform various computations on it. 我现在希望能够参考结果并对其执行各种计算。 If I just assign the query to an object, "newData," it does not let me refer to the elements of this new data table (eg newData.Billed_Amount). 如果仅将查询分配给对象“ newData”,则不允许我引用此新数据表的元素(例如newData.Billed_Amount)。

Is there a quick function like ToList() to make it so that I can work with this new data? 有没有像ToList()这样的快速函数可以使我处理这些新数据?

Here is some sample code: 这是一些示例代码:

let dc = new TypedDataContext()

let newData = query { for x in dc.MyData do
                      where (x.ID = "number of type string")
                      groupBy x.Code into g
                      let average = query { for x in g do
                                            averageBy x.Billed_Amt }
                      select (g, average) }

System.Console.WriteLine(newData)

I now want to, for example, calculate standard deviation within all the groups, but I noticed I cannot perform this computation within a query. 我现在想例如在所有组中计算标准偏差,但是我注意到我无法在查询中执行此计算。 This is where I would like to have the query results referable to work with. 这是我希望查询结果可供参考的地方。

Thanks! 谢谢!

Because IQueryable inherits from IEnumerable , you can use functions in the Seq module such as toList and toArray . 由于IQueryable继承自IEnumerable ,因此可以在Seq模块中使用诸如toListtoArray类的函数。

open System.Linq //for AsQueryable extension method

let q s =
  query {
    for x in s do
    where (x > 1)
    select x
  }

q ([1; 2].AsQueryable()) |> Seq.toList //[2]

Just do this: 只要这样做:

newData |> Seq.toList

The pipe operator |> applies the function on the right to the value on the left. 管道运算符|>将右侧的函数应用于左侧的值。 You could put it into newData if you want it to be a list: 如果希望它成为列表,则可以将其放入newData中:

let newData = query { for x in dc.MyData do
                      ...
                      select (g, average) }
              |> Seq.toList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM