久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Min 或 Max 時如何處理 LINQ 中的空值?

How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
本文介紹了使用 Min 或 Max 時如何處理 LINQ 中的空值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下 Linq 查詢:

I have the following Linq query:

result.Partials.Where(o => o.IsPositive).Min(o => o.Result)

result.Partials.Where(o => o.IsPositive) 不包含元素時,我得到一個異常.除了將操作一分為二并檢查是否為空之外,是否有一種優雅的方法來處理這個問題?我有一堂課充滿了這樣的操作.

I get an exception when result.Partials.Where(o => o.IsPositive) does not contains elements. Is there an elegant way to handle this other than splitting the operation in two and checking for null? I have a class full of operations like this one.

問題與 LINQ to Objects 相關.

The question is related with LINQ to Objects.

這是我得到的異常(翻譯它說:序列為空):

This is the Exception I'm getting (translated it says: The sequence is empty):

推薦答案

Min

計算的簡短總結

- 無中介(例外!)

   var min = result.Partials.Where(o => o.IsPositive).Min(o => o.Result);

這是您的情況:如果沒有匹配的元素,則 Min 調用將引發異常 (InvalidOperationException).

This is your case: if there are no matching elements, then the Min call will raise an exception (InvalidOperationException).

 var min = result.Partials.Where(o => o.IsPositive)
                          .Select(o => o.Result)
                          .DefaultIfEmpty()
                          .Min();

DefaultIfEmpty 將在 0 元素上創建一個枚舉,當列表中沒有元素時.你怎么知道 0 是 Min 還是 0 代表沒有元素的列表?

DefaultIfEmpty will create an enumeration over the 0 element, when there are no elements in the list. How do you know that 0 is the Min or if 0 stands for a list with no elements?

   var min = result.Partials.Where(o => o.IsPositive)
                            .Min(o => (decimal?)o.Result);

這里的 Min 要么是 null(因為它等于 default(decimal?))要么是找到的實際 Min.

Here Min is either null (because that's equal to default(decimal?)) or the actual Min found.

所以這個結果的消費者會知道:

So a consumer of this result will know that:

  1. 當結果為 null 時,列表 沒有元素
  2. 當結果是十進制值時,列表有一些元素,這些元素的Min就是返回值.
  1. When result is null then the list had no elements
  2. When the result is a decimal value then the list had some elements and the Min of those elements is that returned value.

但是,如果這無關緊要,則可以調用 min.GetValueOrDefault(0).

However, when this doesn't matter, then min.GetValueOrDefault(0) can be called.

這篇關于使用 Min 或 Max 時如何處理 LINQ 中的空值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
GetType on Nullable Boolean(可空布爾值上的 GetType)
主站蜘蛛池模板: 精品国产一区二区在线 | 国产精品区二区三区日本 | 一区免费 | 午夜精品一区二区三区在线视频 | www午夜视频 | 国产成人啪免费观看软件 | 羞羞视频免费观 | 国产欧美日韩一区二区三区在线 | 国产精品a久久久久 | 日本久草| 一区二区三区四区国产 | 久久综合激情 | 久久午夜精品 | 国产精品久久久久久久久久三级 | 国产精品久久久久久久久久久久 | 中文字幕在线中文 | 日韩不卡一区二区 | 在线观看国产视频 | 色男人的天堂 | 黄色免费av | 亚洲毛片一区二区 | 国产在线精品区 | 成人在线免费看 | 日韩国产精品一区二区三区 | 草久久久| 欧美在线视频a | 亚洲国产精品久久久久秋霞不卡 | 97avcc| 国产一级淫片免费视频 | 91文字幕巨乱亚洲香蕉 | 成人妇女免费播放久久久 | 毛片软件| 中文字幕乱码亚洲精品一区 | 日韩欧美网| 奇米久久久 | 麻豆久久久久久久 | 国产精品久久av | 91视频在线 | 中文字幕1区2区3区 亚洲国产成人精品女人久久久 | 黄色毛片一级 | 国产精品九九九 |