簡體   English   中英

具有不同鍵和選定值的字典對象列表

[英]List of objects to Dictionary with distinct keys and selected values

我有以下類的對象List

class Entry
{
    public ulong ID {get; set;}
    public DateTime Time {get; set;}
}

該列表包含每個ID值的多個對象,每個對象具有不同的DateTime。

我可以使用Linq將此List<Entry>轉換為Dictionary<ulong, DateTime> ,其中鍵是ID,值是該ID的Min<DateTime>()嗎?

聽起來你想要按ID分組, 然后轉換為字典,這樣你最終每個ID都有一個字典條目:

var dictionary = entries.GroupBy(x => x.ID)
                        .ToDictionary(g => g.Key,
                                      // Find the earliest time for each group
                                      g => g.Min(x => x.Time));

要么:

                         // Group by ID, with each value being the time
var dictionary = entries.GroupBy(x => x.ID, x => x.Time)
                         // Find the earliest value in each group
                        .ToDictionary(g => g.Key, g => g.Min())

組列表<object>帶有鍵和不同值的計數<div id="text_translate"><p>我正在嘗試提出一個解決方案來對給定的鍵和值列表進行分組。</p><p> 我需要像這樣對它進行分組:Key &gt; Dicttinct Value &gt; Count。</p><p> 例如</p><pre>XFN99 2 3 &lt;= [CODE] [DISTINCT_VALUE] [OCCURANCE IN LIST]</pre><p> 我有一個返回List&lt;ObjectPart&gt;的方法。 此 object 具有public string code和public int value 。</p><p> 我需要獲取鍵中每個不同值的計數。</p><p> 示例輸入:</p><pre> XFN999 2 XFN999 2 XFN999 2 XFN999 4 XFN999 8 XFN999 8 XFN999 8 BPN655 1 BPN675 2 BPN655 1</pre><p> 所需的 Output:</p><pre> XFN999 2 =&gt; 3x XFN999 4 =&gt; 1x XFN999 8 =&gt; 3x BPN655 1 =&gt; 2x BPN675 2 =&gt; 1x</pre><p> 我試圖 LINQ 以我的方式成功但失敗了,因為它對每個鍵的值求和。</p><pre> var distValsPerKey = S.getAllDiffs().Result.GroupBy(x =&gt; x.code).Select(x =&gt; new { C_ = x.Key, V_ = x.Distinct().Count() });</pre><p> Output 與我的解決方案:</p><pre> BPN373 =&gt; 30 BPN374 =&gt; 35 BPN377 =&gt; 47 BPN378 =&gt; 43 BPN387 =&gt; 67 BPN388 =&gt; 49 BPN653 =&gt; 10 BPN654 =&gt; 15 BPN699 =&gt; 40 BPN700 =&gt; 45 BPN711 =&gt; 68 BPN723 =&gt; 13 BPN724 =&gt; 11 BPN853 =&gt; 5 BPN854 =&gt; 6 BPN877 =&gt; 99 BPN878 =&gt; 94 BPN505 =&gt; 92 BPN507 =&gt; 570 BPN508 =&gt; 617</pre><p> 我的解決方案基本上對 key 的值求和,但<strong>我需要對每個 key 的不同值求和/計數</strong>。</p><p> 下面的例子:</p><p> <strong>Getting input of List&lt;ObjectPart&gt;</strong></p><pre> public async Task&lt;List&lt;ObjectPart&gt;&gt; getAllDiffs() { List&lt;ObjectPart&gt; TEMP = new List&lt;ObjectPart&gt;(); await Task.Run(() =&gt; { using (IngresConnection CONN = new IngresConnection()) { string QUERRY = SELECT_REDACTED; try { using (IngresCommand CMD = new IngresCommand()) { CONN.ConnectionString = "host=; userID=; pwd=; database=;"; CMD.Connection = CONN; CMD.CommandText = QUERRY; CONN.Open(); using (IngresDataReader READ = CMD.ExecuteReader()) { while (READ.Read()) { ObjectPart OP = new ObjectPart(); OP.wenr = READ.GetValue(0).ToString().Trim(); OP.difference = Convert.ToInt32(READ.GetInt32(1)); TEMP.Add(OP); } READ.Close(); CONN.Close(); } } } catch (Exception E) { CONN.Close(); CONN.Dispose(); } } }); List&lt;ObjectPart&gt; OPS_SORTED = TEMP.OrderBy(m =&gt; m.code).ToList(); return OPS_SORTED; }'</pre><p> <strong>LINQ the input to get desired output</strong></p><pre> var distValsPerKey = S.getAllDiffs().Result.GroupBy(x =&gt; x.code).Select(x =&gt; new { C_ = x.Key, V_ = x.Distinct().Count() }); foreach(var OP in distValsPerKey) { Console.WriteLine($"{OP.C_} = &gt; {OP.V_}"); }</pre></div></object>

[英]Group List<Object> with keys and distinct values count

暫無
暫無

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

相關問題 字典值作為不同的鍵 從包含列表並按值過濾的字典中獲取鍵(對象) 對象列表中的不同值 字典鍵和值到選擇列表 從具有對象的對象列表中獲取不同的值 組列表<object>帶有鍵和不同值的計數<div id="text_translate"><p>我正在嘗試提出一個解決方案來對給定的鍵和值列表進行分組。</p><p> 我需要像這樣對它進行分組:Key &gt; Dicttinct Value &gt; Count。</p><p> 例如</p><pre>XFN99 2 3 &lt;= [CODE] [DISTINCT_VALUE] [OCCURANCE IN LIST]</pre><p> 我有一個返回List&lt;ObjectPart&gt;的方法。 此 object 具有public string code和public int value 。</p><p> 我需要獲取鍵中每個不同值的計數。</p><p> 示例輸入:</p><pre> XFN999 2 XFN999 2 XFN999 2 XFN999 4 XFN999 8 XFN999 8 XFN999 8 BPN655 1 BPN675 2 BPN655 1</pre><p> 所需的 Output:</p><pre> XFN999 2 =&gt; 3x XFN999 4 =&gt; 1x XFN999 8 =&gt; 3x BPN655 1 =&gt; 2x BPN675 2 =&gt; 1x</pre><p> 我試圖 LINQ 以我的方式成功但失敗了,因為它對每個鍵的值求和。</p><pre> var distValsPerKey = S.getAllDiffs().Result.GroupBy(x =&gt; x.code).Select(x =&gt; new { C_ = x.Key, V_ = x.Distinct().Count() });</pre><p> Output 與我的解決方案:</p><pre> BPN373 =&gt; 30 BPN374 =&gt; 35 BPN377 =&gt; 47 BPN378 =&gt; 43 BPN387 =&gt; 67 BPN388 =&gt; 49 BPN653 =&gt; 10 BPN654 =&gt; 15 BPN699 =&gt; 40 BPN700 =&gt; 45 BPN711 =&gt; 68 BPN723 =&gt; 13 BPN724 =&gt; 11 BPN853 =&gt; 5 BPN854 =&gt; 6 BPN877 =&gt; 99 BPN878 =&gt; 94 BPN505 =&gt; 92 BPN507 =&gt; 570 BPN508 =&gt; 617</pre><p> 我的解決方案基本上對 key 的值求和,但<strong>我需要對每個 key 的不同值求和/計數</strong>。</p><p> 下面的例子:</p><p> <strong>Getting input of List&lt;ObjectPart&gt;</strong></p><pre> public async Task&lt;List&lt;ObjectPart&gt;&gt; getAllDiffs() { List&lt;ObjectPart&gt; TEMP = new List&lt;ObjectPart&gt;(); await Task.Run(() =&gt; { using (IngresConnection CONN = new IngresConnection()) { string QUERRY = SELECT_REDACTED; try { using (IngresCommand CMD = new IngresCommand()) { CONN.ConnectionString = "host=; userID=; pwd=; database=;"; CMD.Connection = CONN; CMD.CommandText = QUERRY; CONN.Open(); using (IngresDataReader READ = CMD.ExecuteReader()) { while (READ.Read()) { ObjectPart OP = new ObjectPart(); OP.wenr = READ.GetValue(0).ToString().Trim(); OP.difference = Convert.ToInt32(READ.GetInt32(1)); TEMP.Add(OP); } READ.Close(); CONN.Close(); } } } catch (Exception E) { CONN.Close(); CONN.Dispose(); } } }); List&lt;ObjectPart&gt; OPS_SORTED = TEMP.OrderBy(m =&gt; m.code).ToList(); return OPS_SORTED; }'</pre><p> <strong>LINQ the input to get desired output</strong></p><pre> var distValsPerKey = S.getAllDiffs().Result.GroupBy(x =&gt; x.code).Select(x =&gt; new { C_ = x.Key, V_ = x.Distinct().Count() }); foreach(var OP in distValsPerKey) { Console.WriteLine($"{OP.C_} = &gt; {OP.V_}"); }</pre></div></object> 我可以使用對象列表作為字典鍵嗎? 根據屬性值檢索列表中的不同對象 HashSet或Distinct用於讀取對象的List &lt;&gt;中的不同屬性值 如何通過混合值和鍵將Dictionary轉換為List?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM