计算 DataGridView 中以特定值开始的数据重复次数

发布于 2025-01-20 05:53:49 字数 1577 浏览 3 评论 0原文

我的 dataGridView1 包含两列:Col1Col2,它们都包含重复的值。

Col1 | Col2                  Id | Value| Repetition
===========                 =======================
2515 | 1105    ----------     1 | 2515 | 3
1105 | 2515    |button|       2 | 2508 | 1
3800 | 2208    ----------     
2515 | 1105
2508 | 3800

我需要通过仅选择以 25 开头的值来计算两列中值的重复次数,然后在由 Columns: Id 组成的 dataGridView2 中显示结果、ValueRepetition(单击按钮后)。

我尝试了以下逻辑,但我错过了仅选择、计数和显示以 25 开头的值的条件。这对所有值进行计数,并在第二个网格中显示两列中每个值的重复。

            var q1 = dt.AsEnumerable().Select(r => r.Field<string>("Col1")).ToList();
            var q2 = dt.AsEnumerable().Select(r => r.Field<string>("Col2")).ToList();

            List<string> list = new List<string>();
            list.AddRange(q1);
            list.AddRange(q2);
            var result = list.GroupBy(x => x)
            .Select(g => new { Value = g.Key, Count = g.Count() })
            .OrderByDescending(x => x.Count);
            int count = 1;
            dataGridView2.Columns.Add("Id", "");
            dataGridView2.Columns.Add("Value", "");
            dataGridView2.Columns.Add("Repetition", "");

            foreach (var item in result)
            {
                dataGridView2.Rows.Add(count, item.Value, item.Count);
                count++;
            }

如何计算以 25 开头的值并在 dataGridView2 中显示它们的重复(如上所示)?

I have dataGridView1 containing two columns, Col1 and Col2, which both contain duplicate values.

Col1 | Col2                  Id | Value| Repetition
===========                 =======================
2515 | 1105    ----------     1 | 2515 | 3
1105 | 2515    |button|       2 | 2508 | 1
3800 | 2208    ----------     
2515 | 1105
2508 | 3800

I need to count repetition of values from both columns by selecting only values starting with 25 then show the result in dataGridView2 which consists of Columns: Id, Value, and Repetition, after clicking on button.

I tried the following logic but I miss the condition to select, count and show only values start with 25. this count all values and show in 2nd grid the repetition of every value in both columns.

            var q1 = dt.AsEnumerable().Select(r => r.Field<string>("Col1")).ToList();
            var q2 = dt.AsEnumerable().Select(r => r.Field<string>("Col2")).ToList();

            List<string> list = new List<string>();
            list.AddRange(q1);
            list.AddRange(q2);
            var result = list.GroupBy(x => x)
            .Select(g => new { Value = g.Key, Count = g.Count() })
            .OrderByDescending(x => x.Count);
            int count = 1;
            dataGridView2.Columns.Add("Id", "");
            dataGridView2.Columns.Add("Value", "");
            dataGridView2.Columns.Add("Repetition", "");

            foreach (var item in result)
            {
                dataGridView2.Rows.Add(count, item.Value, item.Count);
                count++;
            }

How can I count values start with 25 and show their repetition in dataGridView2as shown above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2025-01-27 05:53:49

我需要的条件是以下内容,它正常运行,如我所期望的

var result = list.Where(x=>x.StartsWith("25")).GroupBy(x => x).Select(g => new { Value = g.Key, Count = g.Count() }).OrderByDescending(x => x.Count); 

The condition I needed was the following and it works as I expected

var result = list.Where(x=>x.StartsWith("25")).GroupBy(x => x).Select(g => new { Value = g.Key, Count = g.Count() }).OrderByDescending(x => x.Count); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文