Can you please help me how to write this query in LINQ syntax
select distinct machine_id , sample_id from results where custid = 18
order by sample_id
I tried this but not working like the query it shows only machine_id in sample no 1 but I need to select distinct machine_id for each sample
public ActionResult Indexs()
{
int UserId = Convert.ToInt32(Session["UserID"]);
var samples = _context.RESULTS.GroupBy(mach => mach.machine_id).Select( x=> x.FirstOrDefault()).Where(x=> x.custid == UserId).ToList();
return View(samples);
}
I cannot find DistinctBy<> only Distinct available I tried the solutions in this question but its not cover my case
https://stackoverflow.com/questions/14321013/distinct-in-linq-based-on-only-one-field-of-the-table
please I need your help and thank you in advance
UPDATE :
Still the solution not working as expected and not show all data this is the code I use :
var samples = _context.RESULTS.GroupBy(mach => new { mach.machine_id, mach.sample_id }).Select(x=>x.FirstOrDefault()).OrderBy(n=> new { n.sample_id,n.program_id }).Where(x => x.custid == UserId).ToList();
This is example what I have in RESULTS table and what I need to select:
user sample no 1 machine_id
1 1 4
1 1 2026
1 1 2030
1 1 2046
1 1 2053
1 1 2058
1 1 2061
1 1 2080
1 1 2081
1 1 2083
1 1 2084
but with this command it shows only 4 machines:
2080
2081
2083
2084
and I think groupby and firstordefault cause this please how to solve it and select distinct machine_id from each sample like the SELECT statement why its very difficult to make simple query in LINQ ?
this is the VIEW code , I dont know if it will be useful :
@model IEnumerable<warehouse.Models.RESULT>
@{
ViewBag.Title = "Indexs";
Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}
<img style="margin-left:250px;" src="~/images/weblogo.png" />
<p style="margin-left:40px;">
<h3 style="margin-left:100px; font-family:Andalus;text-underline-position:below">
@Html.Label("Hospital Name :")
@Html.DisplayFor(model => Model.FirstOrDefault().sys_users.user_full_name)
</h3>
<table class="table table-bordered">
<tr style="background-color:hotpink">
<th>
@Html.DisplayNameFor(model => model.Program.name)
</th>
<th>
@Html.DisplayNameFor(model => model.sample.name)
</th>
<th>
@Html.DisplayNameFor(model => model.Machine.Machine_name)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Program.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.sample.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Machine.Machine_name)
</td>
<td>
@Html.ActionLink("Enter", "Edit", new { id = item.sample_id, programId = item.program_id, custId = item.custid, machineId = item.machine_id }, new { @class = "btn btn-primary" }) |
@Html.ActionLink("Report", "Details", new { id = item.sample_id, programId = item.program_id, custId = item.custid }, new { @class = "btn btn-danger" }) |
@Html.ActionLink("Statistics", "Edit", "Statistics", new { programId = item.program_id, hospitalNo = item.custid }, new { @class = "btn btn-info" })
</td>
</tr>
}
</table>