久久久久久久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狠狠爱com | av看片网站 | 伊人网一区 | 日韩乱码一二三 | 久久6| 丁香综合| 成人在线精品视频 | 国产日屁 | 一本色道精品久久一区二区三区 | 久久精品小视频 | 国产偷录叫床高潮录音 | 国产亚洲精品精品国产亚洲综合 | 天天看天天干 | 国产精品久久久久久久久免费软件 | av在线播放网 | 欧美淫片 | 成人av网站在线观看 | 日本理论片好看理论片 | 亚洲第一区国产精品 | 久久成人精品视频 | 国产精品自拍啪啪 | 中文字幕乱码视频32 | 国产精品久久一区二区三区 | 麻豆一区二区三区 | 色综网 | 国产草草视频 | 中文字幕第一页在线 | 亚洲激情在线 | 日韩一区二区三区av | 欧美久久久久 | 亚洲精品久久久久中文字幕欢迎你 | 成人高清在线视频 | 一区二区三区在线免费观看视频 | 日本a视频 | 久久国产精品72免费观看 | 亚洲综合一区二区三区 | 久久精品综合 | 中文字幕一区二区三区四区 | 日韩av视屏| 亚洲一区二区三区福利 | 中午字幕在线观看 |