将 2 个不同 类 的列表组合成 1 个连接在公共 属性 上的列表

Combine 2 ILists of different classes into 1 IList joined on a common property

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

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

也许我有这样的class:

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 table 将是这样的:

<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 帮助我走上正确的道路。

创建 2 classes

的复合 class
    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>