I have two tables in a single view, with the first one showing the results of a query using this controller:

Controller:

public ActionResult ResultDetails(int? id)
{
var tenders = from t in db.Tenders.Include(t => t.Compendium).Include(t => t.TrainerRegistration) select t;            

tenders = tenders.Where(q => q.Compendium.InstitutionID==id);

return View(tenders.ToList());
}

Displaying the data in the first table is pretty straightforward. However, when I tried using the same scaffolded razor syntax in my second table, this returned duplicate rows particularly in my item2.TrainerRegistration.NTTC column

@foreach (var item2 in Model)
{
    <tr>
        <td nowrap>
            @Html.DisplayFor(modelItem => item2.TrainerRegistration.Trainer.TrainerName)
        </td>
        <td nowrap>
            @Html.DisplayFor(modelItem => item2.TrainerRegistration.Qualification.Title)
        </td>
        <td nowrap>
            @Html.DisplayFor(modelItem => item2.TrainerRegistration.NTTC)
        </td>
    </tr>
}

the query in the controller would really return duplicates, but how can I remove these duplicate rows in the second table? I have tried adding GroupBy() or Distinct() to my NTTC property but I can't get it to work this way or its other variations:

@foreach (var item2 in Model.GroupBy(x => x.TrainerRegistration.NTTC).ToList())

After multiple guesses, altering the argument inside @foreach would always affect the @Html.DisplayFor(modelItem => item2.****) in one way or another. I'm done guessing at this point and would really appreciate it if someone could point me to the correct way of doing this.

1

There are 1 best solutions below

0
On

I was able to resolve my problem by using First(), on rows where TrainerRegistrationID is being repeated

 @foreach (var item2 in Model.GroupBy(x => x.TrainerRegistrationID).Select(y => y.First()))