简体   繁体   English

按不同的整数列表分组

[英]Group by a distinct list of integers

MyObject()
{
     String dept;
     List<int> id;
     Object obj;
}

Using LINQ , how can I return a list of the above objects organized as follows: 使用LINQ ,我如何返回上述对象的列表,其组织如下:

Group all of the obj objects by [ department and EQUAL id list ]. 按[部门和等号列表]将所有obj对象分组。 The list being considered equal if it contains the same numbers, not necessarily the same order(a set). 如果列表包含相同的数字,而不一定是相同的顺序(一组),则认为该列表相等。

GroupBy has an overload that accepts a custom IEqualityComparer<MyObject> . GroupBy具有接受自定义IEqualityComparer<MyObject>重载 Write one that regards two objects as equal when dept is equal and id is set-equal, and pass it as an argument. 编写一个在dept相等且id为set-equal时将两个对象视为相等的对象,并将其作为参数传递。

A convenient way to implement set equality is to write 实现集合相等的一种简便方法是编写

new HashSet(x.id).SetEquals(new HashSet(y.id))

although this will end up being inefficient and probably not the best idea if there are lots of comparisons to make. 尽管最终将导致效率低下,并且如果进行大量比较可能不是最好的主意。

Building off of Jon's answer , if efficiency is an issue, you can store the HashSet for each object in an anonymous object: 基于Jon的答案 ,如果效率存在问题,则可以将每个对象的HashSet存储在匿名对象中:

myObjects.Select(x => new { myObject = x, hashSet = new HashSet(x.id) })
         .GroupBy(x => x.hashSet, HashSet<int>.CreateSetComparer())
         .SelectMany(x => x.GroupBy(y => y.myObject.dept))

If you want to perform only one GroupBy you could store the HashSet in a Tuple or custom class, but then you would have to create your own IEqualityComparer . 如果只想执行一个GroupBy ,则可以将HashSet存储在Tuple或自定义类中,但随后必须创建自己的IEqualityComparer

组列表<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.

相关问题 如何从对象列表中创建一个不同的整数列表? - How to make a distinct list of integers from a list of objects? 如何根据移动范围对整数列表进行分组? - How to group a list of integers based on moving range? 使用带有不同字符串列表的select语句分组 - Group by with a select statement with Distinct list of Strings 如何按不同的列表分组? - How can I group by a distinct list? 组列表<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 将列表分组并仅在输入为列表时获取不同的值 - group a list and only get distinct values when input is a list Linq 加入两个列表,按结果和 Select 不同值分组 - Linq Join Two List , Group By Result And Select Distinct Value Linq查询分组按不同 - Linq query group by distinct Group by Distinct使用Linq - Group by Distinct using Linq 与 Linq 中的 group by 不同 - Distinct with group by in Linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM