簡體   English   中英

如何在 LINQ 中編寫查詢和使用 distinct 和 order by?

[英]how to write query and use distinct and order by in LINQ?

你能幫我用 LINQ 語法寫這個查詢嗎

select distinct machine_id, sample_id from results where custid = 18 order by sample_id

我嘗試了這個,但不像查詢那樣工作,它僅在示例 1 中顯示 machine_id,但我需要為每個示例 select 不同的 machine_id

 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); }

我找不到 DistinctBy<> only Distinct available 我嘗試了這個問題的解決方案,但它不包括我的情況

https://stackoverflow.com/questions/14321013/distinct-in-linq-based-on-only-one-field-of-the-table

請我需要你的幫助,並提前謝謝你

更新:

解決方案仍然沒有按預期工作並且沒有顯示所有數據這是我使用的代碼:

 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();

這是我在 RESULTS 表中的示例以及我需要 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

但使用此命令它只顯示 4 台機器:

2080

2081

2083

2084

我認為 groupby 和 firstordefault 導致這個請如何解決它和 select 從每個樣本中區分 machine_id 就像 SELECT 語句為什么它很難在 Z979E08FD891EBB5526A70C82D741A 中進行簡單查詢?

這是VIEW代碼,我不知道它是否有用:

 @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>

這是一個使用 distinctBy 的示例

Obj obj0 = new() { Id = 1, SimpleId = 1, Name = "name1" }; Obj obj1 = new() { Id = 1, SimpleId = 1, Name = "name2" }; Obj obj2 = new() { Id = 1, SimpleId = 1, Name = "name1" }; Obj obj3 = new() { Id = 1, SimpleId = 1, Name = "name2" }; Obj obj4 = new() { Id = 1, SimpleId = 2, Name = "name4" }; Obj obj5 = new() { Id = 1, SimpleId = 2, Name = "name2" }; Obj obj6 = new() { Id = 1, SimpleId = 2, Name = "name3" }; List<Obj> list = new() { obj0,obj1,obj2, obj3, obj4, obj5, obj6}; var result = list.OrderByDescending(o => o.Id).DistinctBy(p => new{p.SimpleId, p.Name}).OrderBy(o => o.Id); Console.WriteLine( "liste count " + list.Count); int n = 0; foreach (Obj obj in result) { Console.WriteLine(++ n + " " +obj); } record Obj { public int Id { get; set; } public int SimpleId { get; set;} public string Name { get; set; } }

結果顯示

liste count 7 1 Obj { Id = 1, SimpleId = 1, Name = name1 } 2 Obj { Id = 1, SimpleId = 1, Name = name2 } 3 Obj { Id = 1, SimpleId = 2, Name = name4 } 4 Obj { Id = 1, SimpleId = 2, Name = name2 } 5 Obj { Id = 1, SimpleId = 2, Name = name3 }

您會看到所有重復項都已刪除

您可能在查詢中有拼寫錯誤或使用了錯誤的屬性。 下面是得到正確的結果。 我將orderby分成兩部分。

 using System; using System.Linq; using System.Text; using System.Collections; using System.Collections.Generic; using System.Data; namespace ConsoleApp2 { class Program { static void Main(string[] args) { Context _context = new Context() { RESULTS = new List<RESULTS>() { new RESULTS() { user = 1, sample_id = 1, machine_id = 4 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2026 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2024 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2038 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2046 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2053 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2058 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2061 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2080 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2081 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2083 }, new RESULTS() { user = 1, sample_id = 1, machine_id = 2684 } } }; int UserId = 1; var samples = _context.RESULTS.GroupBy(mach => new { mach.machine_id, mach.sample_id }).Select(x => x.FirstOrDefault()).OrderBy(n => n.sample_id ).ThenBy(x => x.machine_id).Where(x => x.user == UserId).ToList(); } } public class Context { public List<RESULTS> RESULTS { get; set; } } public class RESULTS { public int user { get; set; } public int sample_id { get; set; } public int machine_id { get; set; } } }

我發現您的問題帖子(就目前而言)留下了一些可以解釋的部分。

我的理解是顯示的RESULTS選擇

user_id sample_id 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

實際上是您在執行時得到的選擇

select 不同的user_id、sample_id、machine_id
結果
其中user_id = 1
sample_id 排序

,而數據庫中的RESULTS表可能包含比顯示的 11 個更多的條目。

這是一個正確的假設嗎?

(如果不是,請通知我,我將刪除此答案。)

如果是這樣,我相信RESULTS表中可能存在{ sample_id, machine_id }相等但user_id不同的條目。

例如, RESULTS可能包含兩個條目,其中sample_id == 1machine_id == 4

 user_id sample_id machine_id ------------------------------- 2 1 4 1 1 4......... // other entries

當通過{ sample_id, machine_id }對這兩個條目進行分組時,您將得到一個包含兩個元素的分組:

 Key: { 1, 4 } Elements: { 2, 1, 4 }, { 1, 1, 4 }

選擇該分組中的第一個元素將為您提供

{ 2, 1, 4 }

, 僅僅是因為{ 2, 1, 4 } 4 } 在表格中出現{ 1, 1, 4 }之前。 甚至在到達.Where()語句之前,我們基本上都忽略了數據庫表條目{ 1, 1, 4 }

現在,通過使用.Where()僅獲取與user_id == 1關聯的元素,元素{ 2, 1, 4 }將被排除,視為user_id == 2


因此,我的建議是更改方法調用的順序,以便在分組和選擇第一個組元素之前使用.Where() 一個最小的例子:

 var filteredResults = results.Where(r => r.user_id == 1) // filtering before grouping.GroupBy(r => new { r.machine_id, r.sample_id }).Select(r => r.First());

這樣做時,分組將僅基於userId == 1的條目,因此,在選擇每個分組中的第一個元素的階段,與user_id == 1關聯的條目不會“丟失”。

這個小提琴可能有助於顯示有關方法調用順序的差異。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM