如果求和值全为空,如何使linq Sum返回null

时间:2023-02-13 11:50:41

I have a LINQ query that looks like this...

我有一个看起来像这样的LINQ查询...

var duration = Level3Data.AsQueryable().Sum(d => d.DurationMonths);

If all the d.DurationMonths values are null the Sum returns 0. How can I make the Sum return null if all the d.DurationMonths are null? Or do I need to run a separate query first to eliminate this situation before performing the sum?

如果所有d.DurationMonths值都为null,则Sum返回0.如果所有d.DurationMonths都为null,如何使Sum返回null?或者我是否需要首先运行单独的查询以在执行总和之前消除这种情况?

5 个解决方案

#1


11  

Along with the previous suggestion for an extension method - you could use a ternary operator...

与之前的扩展方法建议一起 - 您可以使用三元运算符...

var duration = Level3Data.AsQueryable().Any(d => d.DurationMonths.HasValue) 
               ? Level3Data.AsQueryable().Sum(d => d.DurationMonths) 
               : null;

#2


4  

You can use Aggregate to provide custom aggregation code :

您可以使用Aggregate提供自定义聚合代码:

var items = Level3Data.AsQueryable();
var duration = items.Aggregate<D,int?>(null, (s, d) => (s == null) ? d.DurationMonths : s + (d.DurationMonths ?? 0));

(assuming the items in Level3Data are of type D)

(假设Level3Data中的项目为D类型)

#3


3  

var outputIndicatorSum = (from OutputIndicatorTable in objDataBaseContext.Output_Indicators
                                          where OutputIndicatorTable.Output_Id == outputId
                                          select (int?)OutputIndicatorTable.Status).Sum();
                int outputIndicatorSumReturn = Convert.ToInt32(outputIndicatorSum);
                return outputIndicatorSumReturn;

You can explicitly type cast non-nullable varaible into nullable type. i.e, select (int?)OutputIndicatorTable.Status).Sum();

您可以显式地将强制转换非可空变量类型转换为可空类型。即,select(int?)OutputIndicatorTable.Status).Sum();

#4


1  

Using Sum alone, this is impossible. As you indicated in your question, you will need to check for this situation before you call Sum:

单独使用Sum,这是不可能的。正如您在问题中指出的那样,在致电Sum之前,您需要检查这种情况:

var q = Level3Data.AsQueryable();
var duration = q.All(d => d.DurationMonths == null)
                   ? null
                   : q.Sum(d => d.DurationMonths);

#5


-1  

If you would like the result without two queries try:

如果您希望结果没有两个查询,请尝试:

var duration = Level3Data.AsQueryable().Sum(d => (double?)d.DurationMonths);

var duration = Level3Data.AsQueryable()。Sum(d =>(double?)d.DurationMonths);

If you want zero instead of null as the result of this query use:

如果您希望此查询的结果为零而不是null,请使用:

var duration = Level3Data.AsQueryable().Sum(d => (double?)d.DurationMonths) ?? 0;

var duration = Level3Data.AsQueryable()。Sum(d =>(double?)d.DurationMonths)?? 0;

#1


11  

Along with the previous suggestion for an extension method - you could use a ternary operator...

与之前的扩展方法建议一起 - 您可以使用三元运算符...

var duration = Level3Data.AsQueryable().Any(d => d.DurationMonths.HasValue) 
               ? Level3Data.AsQueryable().Sum(d => d.DurationMonths) 
               : null;

#2


4  

You can use Aggregate to provide custom aggregation code :

您可以使用Aggregate提供自定义聚合代码:

var items = Level3Data.AsQueryable();
var duration = items.Aggregate<D,int?>(null, (s, d) => (s == null) ? d.DurationMonths : s + (d.DurationMonths ?? 0));

(assuming the items in Level3Data are of type D)

(假设Level3Data中的项目为D类型)

#3


3  

var outputIndicatorSum = (from OutputIndicatorTable in objDataBaseContext.Output_Indicators
                                          where OutputIndicatorTable.Output_Id == outputId
                                          select (int?)OutputIndicatorTable.Status).Sum();
                int outputIndicatorSumReturn = Convert.ToInt32(outputIndicatorSum);
                return outputIndicatorSumReturn;

You can explicitly type cast non-nullable varaible into nullable type. i.e, select (int?)OutputIndicatorTable.Status).Sum();

您可以显式地将强制转换非可空变量类型转换为可空类型。即,select(int?)OutputIndicatorTable.Status).Sum();

#4


1  

Using Sum alone, this is impossible. As you indicated in your question, you will need to check for this situation before you call Sum:

单独使用Sum,这是不可能的。正如您在问题中指出的那样,在致电Sum之前,您需要检查这种情况:

var q = Level3Data.AsQueryable();
var duration = q.All(d => d.DurationMonths == null)
                   ? null
                   : q.Sum(d => d.DurationMonths);

#5


-1  

If you would like the result without two queries try:

如果您希望结果没有两个查询,请尝试:

var duration = Level3Data.AsQueryable().Sum(d => (double?)d.DurationMonths);

var duration = Level3Data.AsQueryable()。Sum(d =>(double?)d.DurationMonths);

If you want zero instead of null as the result of this query use:

如果您希望此查询的结果为零而不是null,请使用:

var duration = Level3Data.AsQueryable().Sum(d => (double?)d.DurationMonths) ?? 0;

var duration = Level3Data.AsQueryable()。Sum(d =>(double?)d.DurationMonths)?? 0;