简体   繁体   English

如何设置动态linq查询中使用的聚合函数的结果?

[英]How to set round of the result of aggregate function used in dynamic linq query?

Below is my code: 下面是我的代码:

sFieldList.Select(y => "Sum(Convert.ToDouble(iif(it[\""+y+"\"] == @0,0,it[\""+y+"\"]))) as "+y)and then 

var newSort = dataTable
                .AsEnumerable()
                .AsQueryable()
                .GroupBy("new("+gField+")", "it")
                .Select("new("+sField+",it.Key as Key, it as Data)",DBNull.Value);

I wish to round of the result of Sum() method above to 2 decimal digits. 我希望将上述Sum()方法的结果四舍五入为2个十进制数字。 How can I add it in this query itself? 如何在查询本身中添加它?

Hopefully I'm understanding your problem correctly, but as far as I can tell, you just need an additional Select() 希望我能正确理解您的问题,但据我所知,您只需要一个附加的Select()

    var roundedSums = list.Select(x => "some dynamic query on x")
                          .Select(x => Math.Round(x, 2);

By chaining multiple Selects(), you are simply projecting your original set on a record to record basis. 通过链接多个Selects(),您可以简单地将原始集投影到记录中。

If this isn't what you're looking for, we may need a bit more clarification. 如果这不是您想要的,我们可能需要更多说明。

Somehow dynamic linq does not always understand the numbers in terms of their, so try this: 动态linq并不总是以数字的方式理解数字,因此请尝试以下操作:

sFieldList.Select(y => "Sum(Math.Round(Convert.ToDouble(iif(it[\""+y+"\"] == @0,0,it[\""+y+"\"])),@1)) as "+y)

and then 接着

var newSort = dataTable
            .AsEnumerable()
            .AsQueryable()
            .GroupBy("new("+gField+")", "it")
            .Select("new("+sField+",it.Key as Key, it as Data)",DBNull.Value,2);

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

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