使用 LinQ 的简单数学问题、简单的 Join 查询、Null 等
我有 2 张桌子
1. 客户端
2. 操作
操作可能导致: 贷项或借项(字符字段中的“C”或“D”)以及日期和金额字段。
我必须使用 linQ 计算每个客户帐户的余额...结果还应该为未进行操作的客户显示余额 0,但
我有以下带有 linQ 语句的函数,但我知道它可以以更好的方式完成更快、更短的路,对吧?会是哪一个?
public static double getBalance(ref ClasesDeDatosDataContext xDC, string SSN,
int xidClient)
{
var cDebits =
from ops in xDC.Operations
where (ops.idClient == xidClient) && (ops.OperationCode == 'D')
select ops.Ammount;
var cCredits =
from ops in xDC.Operations
where (ops.idClient == xidClient) && (ops.OperationCode == 'C')
select ops.Ammount;
return (double)(cCredits.Sum() - cDebits.Sum());
}
谢谢 !!!
I have 2 tables
1. Client
2. Operations
Operations can result in: Credits or Debits ('C' or 'D' in a char field) along with date and ammount fields.
I must calculate the balance for each client's account using linQ... The result should also show balance 0 for clients whom didn't make operations yet
I have the following function with linQ statement, but I know it could be done in a better and quicker, shorter way, right? Which will be?
public static double getBalance(ref ClasesDeDatosDataContext xDC, string SSN,
int xidClient)
{
var cDebits =
from ops in xDC.Operations
where (ops.idClient == xidClient) && (ops.OperationCode == 'D')
select ops.Ammount;
var cCredits =
from ops in xDC.Operations
where (ops.idClient == xidClient) && (ops.OperationCode == 'C')
select ops.Ammount;
return (double)(cCredits.Sum() - cDebits.Sum());
}
Thanks !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道 LINQ-to-SQL 引擎是否可以处理该表达式,但类似这样的事情应该是可能的:
或者也许:
如果集合为空,则 Sum 方法返回零,这样就可以处理没有事务的客户端。
编辑:
更正查询中的拼写:Ampont ->数量
I don't know if the LINQ-to-SQL engine can handle the expression, but something like this should be possible:
Or perhaps:
If the collection is empty, the Sum method returns zero, so that takes care of clients without transactions.
Edit:
Corrected spelling in query: Ampont -> Amount