与LINQ to SQL的总和列 [英] sum column with linq to sql

查看:168
本文介绍了与LINQ to SQL的总和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据视图中,我想总结了一个名为金额

I have a DataView in which I would like to sum a column called "Amount"

现在我知道我可以遍历直通列,并得到了一笔,但我列想知道是否有可能使用LINQ到SQL?

Now I know I can iterate thru the columns and get the sum but I was wondering if is possible using Linq to Sql?

字符串总和= Sql的Linq的东西在这里(不必是字符串,可以是任何类型)

String sum = Linq to Sql stuff here (doesn't have to be a string, can be any type)

谢谢,
rodchar

Thanks, rodchar

推荐答案

假设金额列双(可能是另一种类型)

Assuming the Amount column is a double (could be another type)

double sum = Table.Select(t => t.Amount ?? 0).Sum();

double sum = Table.Sum(t => t.Amount ?? 0).Sum();



使用空coelescing接线员会给你的默认值为0,如果t.Amount为null。

Using the null coelescing operator will give you a default of 0 if t.Amount is null.

这篇关于与LINQ to SQL的总和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆