如何建立一个柱状图为int在C#中的列表 [英] How to build a histogram for a list of int in C#

查看:98
本文介绍了如何建立一个柱状图为int在C#中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
查找数组使用LINQ


最频繁的号码



我有一个整型列表,列表与LT; INT> demoList ,这是像 {1,2,1,1,1,3,2,1} ,我想写一个LINQ语句从该名单,这在我的情况是 1 appearences次数最多的获得次数。


解决方案

  INT highestAppearanceNum = demoList.GroupBy(I = I标记)
.OrderByDescending(GRP => grp.Count())
。选择(GRP => grp.First())
。首先();



修改:如果你也想知道哪些号码出现的频率:

  VAR出场= demoList.GroupBy(I = I标记)
.OrderByDescending(GRP => GRP。 COUNT())
。选择(GRP =>新建{NUM = grp.Key,计数= grp.Count()});
如果(appearances.Any())
{
INT highestAppearanceNum = appearances.First()民。 // 1
INT highestAppearanceCount = appearances.First()计数。 // 5
}


Possible Duplicate:
Find the most frequent numbers in an array using LINQ

I have a list of int, List<int> demoList, which is something like {1, 2, 1, 1, 1, 3, 2, 1} and I want to write a LINQ statement for obtaining the number with the highest number of appearences from that list, which in my case is 1.

解决方案

 int highestAppearanceNum = demoList.GroupBy(i => i)
            .OrderByDescending(grp => grp.Count())
            .Select(grp => grp.First())
            .First();

Edit: If you also want to know which number appears how often:

var appearances = demoList.GroupBy(i => i)
    .OrderByDescending(grp => grp.Count())
    .Select(grp => new { Num = grp.Key, Count = grp.Count() });
if (appearances.Any())
{
    int highestAppearanceNum = appearances.First().Num;     // 1
    int highestAppearanceCount = appearances.First().Count; // 5
}

这篇关于如何建立一个柱状图为int在C#中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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