遍历嵌套列表

时间:2019-04-01 12:56:20

标签: c# nested-loops

我在模型中有以下列表:

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

Dto如下:

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; }
}

返回嵌套列表的查询如下:

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

我正在按SalesLineId进行分组。

在我的cshtml中,我需要遍历嵌套列表。 我有以下代码:

@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>
}

但是,我在循环第二个列表时遇到问题。
当我执行model.ListTransport [i] .SalesLines [k]时,无法直接访问嵌套列表。
知道怎么做吗?

2 个答案:

答案 0 :(得分:0)

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

列表(List<List<>>)的列表就像一个二维数组。 在我的示例中,k是嵌套列表上的迭代器。

答案 1 :(得分:0)

您可以在LINQ this answer explains how it works中使用SelectMany

var salesLineDtos = SalesLines.SelectMany(sl => sl);
foreach (var salesLineDto in salesLineDtos)
{
    Console.WriteLine(salesLineDto.AgentSalesLineId);
    // Access other properties
}