I am using the spreadsheetlight library to access excel files.
How can I shorten the following construct through extension methods and lambda expressions ? I want to count all cells with boolean values.
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++;
}
}
}
You can use LINQ for this:
By
SelectMany(x => x.Values)
we create anotherIEnumerable
that enumerates over allSLCell Cell
s.Then the
Count(x => x.DataType == CellValues.Boolean)
counts the number of cells where the.DataType
isCellValues.Boolean
.