如何从List< List< string>>获取值 [英] How to get value from List<List<string>>

查看:122
本文介绍了如何从List< List< string>>获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有以下收藏



列表< List< string>> Test_List =  new 列表< List< string>>(); 

List< string> a = new 列表< string> { 1 2 1 1 3};
List< string> b = new 列表< string> { 11 12 11 11 13};
List< string> c = new 列表< string> { 21 22 21 21 23};
List< string> d = new 列表< string> { 31 32 31 31 33};

Test_List.Add(a);
Test_List.Add(b);
Test_List.Add(c);
Test_List.Add(d);





我想获得一个新列表,其中包含一个重复最高时间值的Test_list中每个元素的值,例如:在上面的情况下,a,b,c,d是Test_list的元素,我们看到1在a中重复了最高时间,如b中的明智11,c中21和d中的31。

我想要获取一个包含1,11,21,31的新列表或数组。我想要没有任何循环的Linq解决方案。



我试过

  var  res = Test_List.Select(n => n)。选择(l => l)。GroupBy(m = >  m)。选择(g = >   new  {count = g.Count(), value  = g.Key})。OrderByDescending(u = >  u.count)。选择(h => h。 value )。ToArray(); 





但是上面的代码没有不能为我工作。

谢谢

解决方案

你差不多了:

  var  res = Test_List.Select(tl = >  tl.GroupBy(t = >  t)。选择(g = >   new  {val = g.Key,cnt = g.Count ()})。OrderByDescending(o = >  o.cnt)。ToArray()[ 0 ] .val); 


好吧,一个解决方案可能如下:

 List< string> ; doubles = Test_List.SelectMany(l = >  l)。ToLookup(s = >  s)。其中(g = >  g.Count()>   1 )。选择(g = >  g.Key)。ToList(); 

// 完整的分步说明:

// 首先,我们使用SelectMany将所有列表中的字符串组合到一个列表中。
IEnumerable< string> first = Test_List.SelectMany(l = > l);
// 接下来,我们创建一个查找,将字符串作为键,并将该字符串的所有匹配项都显示为值。
ILookup< string,string> next = first.ToLookup(s = > s);
// 之后我们过滤此查找,获取具有多于1个值的所有条目(意味着多于一个)
// 在原始列表中出现。)
IEnumerable< ; IGrouping< string,string>> afterThat = next.Where(g = > g.Count()> 1 );
// 然后我们使用查找键选择原始字符串。
IEnumerable< string> then = afterThat.Select(g = > g.Key);
// 最后我们创建一个列表(或数组)来保存项目。您可以添加订购等。
List< string> last = then.ToList();

我不是说这是最好的方法(LINQ通常有更多的方法可以得到相同的结果),但这是一个解决方案,我认为这并不难理解:)


另一种方法,2个步骤,简单的逻辑,也许它可以为某人工作



 Test_List.ForEach(p = >  p.Sort((x,y)= >  p.Count(s = >  s == y).CompareTo(p.Count(s = >  s == x)))); 
var q = Test_List.Select(p = > p.First())。 ToList();


Hi I have following collection

List<List<string>> Test_List = new List<List<string>>();

           List<string> a = new List<string> { "1", "2", "1", "1", "3" };
           List<string> b = new List<string>{ "11", "12", "11", "11", "13" };
           List<string> c = new List<string>{ "21", "22", "21", "21", "23" };
           List<string> d = new List<string> { "31", "32", "31", "31", "33" };

           Test_List.Add(a);
           Test_List.Add(b);
           Test_List.Add(c);
           Test_List.Add(d);



I want to get a new list that would contain a value repeated highest time in each element of Test_list for e.g. In above case a,b,c,d are element of Test_list, we see 1 has repeated highest time in a, like wise 11 in b, 21 in c and 31 in d.
I want to get a new list or array that would contain 1,11,21,31 in it. I want Linq solution without any loop.

I tried with

var res = Test_List.Select(n=>n).Select(l=>l).GroupBy(m => m).Select(g => new { count = g.Count(), value = g.Key }).OrderByDescending(u=> u.count).Select(h=>h.value).ToArray();



But above code did not work for me.
Thanks

解决方案

You were nearly there:

var res = Test_List.Select(tl => tl.GroupBy(t => t).Select(g => new { val = g.Key, cnt = g.Count() }).OrderByDescending(o => o.cnt).ToArray()[0].val);


Well, one solution could be the following:

List<string> doubles = Test_List.SelectMany(l => l).ToLookup(s => s).Where(g => g.Count() > 1).Select(g => g.Key).ToList();

// Complete step-by-step explanation:

// First we use SelectMany to combine the strings in all our lists to a single list.
IEnumerable<string> first = Test_List.SelectMany(l => l);
// Next we create a lookup, having the string as a key and all the occurrences of that string as a value.
ILookup<string, string> next = first.ToLookup(s => s);
// After that we filter this lookup grabbing all entries that has more than 1 value (meaning more than one
// occurrence in the original list).
IEnumerable<IGrouping<string, string>> afterThat = next.Where(g => g.Count() > 1);
// Then we select the original string, using the keys of the lookup.
IEnumerable<string> then = afterThat.Select(g => g.Key);
// Last we create a list (or array) to hold the items. You could add ordering etc.
List<string> last = then.ToList();

I'm not saying this is the best way to do it (with LINQ there are usually more methods to get to the same result), but it's one solution and I think it's not to hard to understand :)


Another approach, 2 steps, straightforward logic, maybe it does the job for someone

Test_List.ForEach(p => p.Sort((x, y) => p.Count(s => s == y).CompareTo(p.Count(s => s == x))));
var q = Test_List.Select(p => p.First()).ToList();


这篇关于如何从List&lt; List&lt; string&gt;&gt;获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆