Search code examples
c#nested-loops

Looping through a nested list


I have the following list in a model:

public List<List<SalesLineDto>> SalesLines { get; set; }

The Dto is as follows:

public class SalesLineDto
{
    public long AgentSalesLineId { get; set; }
    public long? SalesLineId { get; set; }
    public string Label { get; set; }
    public long? OptionId { get; set; } 
    public double? Quoted { get; set; }
    public string SelectedCurrency { get; set; }
    public long? SelectedPricingMethod { get; set; }
    public long? ComplementaryServiceId { get; set; }
    public string IsOther { get; set; }
}

The query returning the nested list is as follows:

return query.GroupBy(u => u.SalesLineId).Select(grp => grp.ToList()).ToList();

I am doing a grouping by SalesLineId.

In my cshtml, I need to go throught the nested list. I have the following code:

@for (var k = 0; k < Model.ListTransport[i].SalesLines.Count; k++)
{
    <div class="form-control-group">
        <div class="form-control form-control--third-width">
            <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
                @Html.EditorFor(Model => Model.ListTransport[i].SalesLines[k].FirstOrDefault().Label, new { htmlAttributes = new { @class = "mdl-textfield__input", @maxlength = "150", @readonly = "readonly" } })
            </div>
        </div>


            <div class="form-control form-control--combo form-control--third-width">

                @{ var options = Model.ListTransport[i].ListOptions; }
                @for (int j = 0; j < options.Count; j++)
                {
                    @Html.HiddenFor(model => model.ListTransport[i].ListOptions[j].Id)
                    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
                        <span class="label label--floating text-right">@WebMVCStrings.Option @options[j].OptionId</span>
                        @Html.EditorFor(model => model.ListTransport[i].SalesLines[k].ElementAtOrDefault(j).Quoted, new { htmlAttributes = new { @class = "mdl-textfield__input text-right", @maxlength = "150" } })
                        <label class="mdl-textfield__label text-right" for="metricsServiceOption1">@options[j].OptionValue @options[j].OptionUnit</label>
                    </div>
                }
            </div>

        <div class="form-control form-control--combo form-control--third-width">
            <div class="mdl-textfield mdl-textfield--floating-label js-dropdown">
                @(Html.EJ().DropDownListFor(model => model.ListTransport[i].SalesLines[k].ElementAtOrDefault(0).SelectedCurrency)
                .Datasource((List<KeyValuePair>)ViewBag.ListCurrencies)
                .DropDownListFields(Currency => Currency.Text("Value").Value("Value"))
                .EnableFilterSearch(true)
                .FilterType(SearchFilterType.Contains)
                .PopupHeight("300px")
                .ClientSideEvents(ev => ev.Change("updateCurrency"))
                .CssClass("currencyValue" + Model.ListTransport[i].ModOfTransportId))
                <label class="mdl-textfield__label" for="SelectedCurrency">@WebMVCStrings.Currency</label>
            </div>

            <div class="mdl-textfield mdl-textfield--floating-label js-dropdown">
                @(Html.EJ().DropDownListFor(model => model.ListTransport[i].SalesLines[k].FirstOrDefault().SelectedPricingMethod)
                .Datasource((List<KeyValuePair>)ViewBag.ListBillingUnits)
                .DropDownListFields(BillingUnit => BillingUnit.Text("Value").Value("Key"))
                .EnableFilterSearch(true)
                .FilterType(SearchFilterType.Contains)
                .PopupHeight("300px")
                .ClientSideEvents(ev => ev.Change("updateBillingUnit"))
                .CssClass("billingUnitValue"))
                <label class="mdl-textfield__label" for="SelectedCurrency">@WebMVCStrings.BillingUnit</label>
            </div>
        </div>
    </div>
}

However, I am having issues to loop throught the 2nd list.
When I do model.ListTransport[i].SalesLines[k], I can't access to the nested list directely.
Any idea of how to do that?


Solution

  •  Model.ListTransport[i].SalesLines[j][k]
    

    A list of lists (List<List<>>) is like a two dimensional array. In my example, k is the iterator on the nested list.