本文介绍了LINQ - 将号码存储为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

PK    OrderNumber      USERDEFFIELD
1     0001             10
2     0001             25
3     0002             20
4     0002             22
5     0002             NULL
6     0003             ABC123

UserDefField列在数据库中为VARCHAR类型。使用LINQ,如何获得每个订单的SUM(UserDefField)? UserDefField的NULL和非数值值被认为是零。结果我试图得到:

The UserDefField column is of VARCHAR type in the database. Using LINQ, how can I get the SUM(UserDefField) per order? NULL and non-numeric values for UserDefField are to be considered as zero. The result I'm trying to get:

OrderNumber  TotalQty
0001         35
0002         42
0003          0

如果UserDefField是严格为空的数字字段我知道我会在foreach循环中执行此操作:

If UserDefField is strictly nullable numeric field I know I would do this inside a foreach loop:

 TotalQtyForThisOrder = orders.Sum(w => w.UserDefField ?? 0 );

但是对于字符串字段,应该做什么?非常感谢您的帮助。

But for a string field, what should be done? Thank you very much for the help.

推荐答案

TotalQtyForThisOrder = orders.Sum( w = > {
            int result;
            Int32.TryParse(w.UserDefField, out result);
            return result;
});

这篇关于LINQ - 将号码存储为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:06