簡體   English   中英

擴展方法和lambda表達式

[英]Extension methods and lambda expressions

我正在使用試算表庫訪問Excel文件。

如何通過擴展方法和lambda表達式縮短以下構造? 我想用布爾值計算所有單元格。

Dictionary<int, Dictionary<int, SLCell>> cells = sl.GetCells();

int nCount = 0;

foreach (Dictionary<int, SLCell> Value in cells.Values)
{
    foreach (SLCell Cell in Value.Values)
    {
        if (Cell.DataType == CellValues.Boolean)
        {
            nCount++;
        }
    }
}

您可以為此使用LINQ:

int ncount = cells.Values.SelectMany(x => x.Values)
                         .Count(x => x.DataType == CellValues.Boolean);

通過SelectMany(x => x.Values)我們創建另一個枚舉所有SLCell CellIEnumerable

然后Count(x => x.DataType == CellValues.Boolean)計算.DataTypeCellValues.Boolean的單元格數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM