【问题标题】:Combine 2 ILists of different classes into 1 IList joined on a common property将 2 个不同类别的列表合并为 1 个列表,加入一个公共属性
【发布时间】:2020-07-01 18:02:42
【问题描述】:

我有 2 个填写的 IList 供应商和 VwSrmAhmSuppliers,它们具有共同的属性 Supplier.SupplierNo 和 VwSrmAhmSupplier.AhmSupplierNo。我想将该公共属性上的它们组合到一个 IList 中,其中各个类属性仍然可以访问,因此我可以在视图中有效地访问它们。

因此,如果我提取 CommonList 中的第一项,然后询问 CommonList(1).Supplier.SupplierNo 和 CommonList(1).VwAhmSrmSupplier.AhmSupplierNo - 这两个字段将是相同的。

也许我有这样的课程:

public class SupplierDetail
{
    public Supplier Supplier { get; set; }
    public Models.ExternalData.VwSrmAhmSupplier VwSrmAhmSupplier { get;set;}
}
public IList<Supplier> Suppliers { get;set; }
public IList<Models.ExternalData.VwSrmAhmSupplier> VwSrmAhmSuppliers { get; set; }
public IList<SupplierDetail> SupplierDetails;

public async Task OnGetAsync(Boolean? All)
{
   //don't show all records unless explicity asked to!
   if (All == true)
   {
      Suppliers = await _context.Supplier
         .Include(s => s.Status)
         .Include(c => c.Category)
         .Include(c => c.Comments)
         .OrderByDescending(c => c.CreateDate)
         .ToListAsync();

      var supplierNos = Suppliers.Select(s => s.SupplierNo).ToList();

      VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers
         .Where(v => supplierNos
            .Any(s => s == v.AhmSupplierNo))
      .ToListAsync();

      SupplierDetails = ??

}

我试图生成的 HTML 表是这样的:

<tbody>
   @foreach (var item in Model.CommonList)
   {
      <tr>
         <td>
            <a asp-page="./Details" asp-route-id="@item.Id">@Html.DisplayFor(modelItem => item.Supplier.SupplierNo)</a>
         </td>
         <td>
            @Html.DisplayFor(modelItem => item.VwSrmAhmSupplier.AhmSupplierNm)
         </td>
         <td>
            @Html.DisplayFor(modelItem => item.Supplier.CreateDate)
         </td>
         <td>
            @Html.DisplayFor(modelItem => item.Supplier.Creator)
         </td>
         <td>
            @Html.DisplayFor(modelItem => item.Supplier.Category.Description)
         </td>
         <td>
            @Html.DisplayFor(modelItem => item.Supplier.Status.Description)
         </td>
         <td>
            <a asp-page="./Delete" asp-route-id="@item.Id" class="btn btn-danger">Hide</a>
         </td>
      </tr>
   }
</tbody>

【问题讨论】:

  • 问题出在哪里?创建包含这两个字段的类
  • @Selvin 我已将 SupplierDetail 类添加到帖子中并更新了代码。我如何将这些类的列表填充为供应商和 VwSrmAhmSupplier 的复合列表加入 SupplierNo 和 AhmSupplierNo?

标签: c# html linq razor-pages


【解决方案1】:

感谢@Selvin 帮助我走上正确的道路。

创建一个包含 2 个类的复合类

    public class SupplierDetail
    {
        public Supplier Supplier { get; set; }
        public VwSrmAhmSupplier VwSrmAhmSupplier { get;set;}
    }

然后,通过循环填充列表:

            SupplierDetails = new List<SupplierDetail>();

            foreach (Supplier supplier in Suppliers)
            {
                SupplierDetail supplierDetail = new SupplierDetail
                {
                    Supplier = supplier,
                    VwSrmAhmSupplier = VwSrmAhmSuppliers.Where(s => s.AhmSupplierNo == supplier.SupplierNo).First()
                };
                SupplierDetails.Add(supplierDetail);
            }

最后,在视图中访问您的对象属性

    <table id="table" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.SupplierNo)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SupplierDetails[0].VwSrmAhmSupplier.AhmSupplierNm)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.CreateDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Creator)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Category)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SupplierDetails[0].Supplier.Status)
                </th>

                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.SupplierDetails)
            {
            <tr>
                <td>
                    <a asp-page="./Details" asp-route-id="@item.Supplier.Id">@Html.DisplayFor(modelItem => item.Supplier.SupplierNo)</a>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.VwSrmAhmSupplier.AhmSupplierNm)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Supplier.CreateDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Supplier.Creator)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Supplier.Category.Description)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Supplier.Status.Description)
                </td>
                <td>
                    <a asp-page="./Delete" asp-route-id="@item.Supplier.Id" class="btn btn-danger">Hide</a>
                </td>
            </tr>
            }
        </tbody>
    </table>

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多