Объединить 2 IList разных классов в 1 IList, объединенных общим свойством - PullRequest
0 голосов
/ 20 марта 2020

У меня есть 2 заполненных списка поставщиков IList и VwSrmAhmSuppliers с общим свойством Supplier.SupplierNo и VwSrmAhmSupplier.AhmSupplierNo. Я хотел бы объединить их в этом общем свойстве в один IList, где свойства отдельных классов все еще доступны, чтобы я мог эффективно обращаться к ним в представлении.

Так что, если я вытащил первый элемент в CommonList и затем спросил для CommonList (1) .Supplier.SupplierNo и CommonList (1) .VwAhmSrmSupplier.AhmSupplierNo - эти 2 поля будут одинаковыми.

Возможно, у меня есть такой класс:

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>

1 Ответ

0 голосов
/ 20 марта 2020

Спасибо @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>
...