在 Blazor 页面中测试集合中的第一项

Test for first item in a collection in Blazor page

我正在尝试测试项目集合中的第一项。 在下面的代码中,我希望带有文本“first”的 div 呈现一次。在我当前的代码中,它永远不会呈现。我做错了什么?

 <table class="mud-pivot-root">
            @if (Matrix?.Rows?.Any() ?? false)
            {
                int rowCounter = 0;

                @foreach (MatrixRow row in Matrix.Rows)
                {
                    int colCounter = 0;

                    <MudTr>
                        @foreach (MatrixCell c in row.Cells)
                        {
                            int rc = rowCounter;

                            if (rc == 0)
                            {
                                <div>first</div>
                            }

参考:

我认为你的问题是 rc 是一个局部变量并且永远不会设置为 != 0。因此 @if 块中的条件始终为真。

 <table class="mud-pivot-root">
            @if (Matrix?.Rows?.Any() ?? false)
            {
                int rowCounter = 0;

                @foreach (MatrixRow row in Matrix.Rows)
                {
                    int colCounter = 0;

                    <MudTr>
                        @foreach (MatrixCell c in row.Cells)
                        {
                            int rc = rowCounter;

                            if (rc == 0)
                            {
                                <div>first</div>
                               // you need to set rc   
                               // to a value =! 0
                               rc = 1
                            }

另一种方法是在 System.Linq 中使用 Select() 的重载,允许通过合并元素的索引将序列的每个元素投影到新形式中。

    public static IEnumerable<TResult> Select<TSource,TResult>  
        (this IEnumerable<TSource> source, Func<TSource,int,TResult> selector);

您可以使用此方法产生您想要的结果。

    <h3>Matrix</h3>

    <table>
        @if (MyMatrix.Rows.Any())
        {
            @foreach (IndexedMatrixRow indexedRow in 
                        MyMatrix.Rows.Select<MatrixRow, IndexedMatrixRow>((row, index) => new(row, index)))
            {
                <tr>
                    @foreach (IndexedMatrixCell indexedCell in 
                                indexedRow.Row.Cells.Select<MatrixCell, IndexedMatrixCell>((cell, index) => new(cell, index)))
                    {
                        <td>
                            @if (indexedCell.Index == 0)
                            {
                                <p>first</p>
                            }
                            @indexedCell.Cell.CellValue
                        </td>
                    }
                </tr>
            }
        }
    </table>

    @code {
        protected override void OnInitialized()
        {
            var row1 = new MatrixRow();
            row1.Cells.Add(new MatrixCell() { CellValue = "A1" });
            row1.Cells.Add(new MatrixCell() { CellValue = "B1" });
            row1.Cells.Add(new MatrixCell() { CellValue = "C1" });

            var row2 = new MatrixRow();
            row2.Cells.Add(new MatrixCell() { CellValue = "A2" });
            row2.Cells.Add(new MatrixCell() { CellValue = "B2" });
            row2.Cells.Add(new MatrixCell() { CellValue = "C2" });

            MyMatrix.Rows.Add(row1);
            MyMatrix.Rows.Add(row2);

            base.OnInitialized();
        }

        Matrix MyMatrix = new Matrix();

        record IndexedMatrixRow(MatrixRow Row, int Index);
        record IndexedMatrixCell(MatrixCell Cell, int Index);

        class Matrix
        {
            IList<MatrixRow> _rows = new List<MatrixRow>();
            public IList<MatrixRow> Rows => _rows;
        }

        class MatrixRow
        {
            IList<MatrixCell> _cells = new List<MatrixCell>();
            public IList<MatrixCell> Cells => _cells;
        }

        class MatrixCell
        {
            public string CellValue { get; set; }
        }
    }