如何在ASP.NET C#中获取具有重复值的重复计数?

huangapple go评论68阅读模式
英文:

how do get a duplicate count with a duplicate value in ASP.NET C#

问题

如何在ASP.NET C#中获取具有重复值的重复计数,
因此,我不需要SQL部分,因为我在后台使用内置数据

示例:

我有以下数据,

|      ID		      |     尺寸	     |      |
|---------------------|------------------|------|
|    1000984958       |         XS       |---不同ID不同尺寸(ID计数1)|
|    1000972994	      |         S        |---相同ID不同尺寸(ID计数2)|
|    1000972994	      |         L        |---相同ID不同尺寸(ID计数2)|

结果:

|ID		    | 尺寸	| 计数|
|-----------|-------|------|
|1000984958	|  XS	|    1 |
|1000972994	|  S	|    2 |
|1000972994	|  L	|    2 |
英文:

How can I get duplicate count with duplicate value in ASP.NET C#,
so, I don't need SQL part because I use data in buil on the background

Example:

I have below data,

ID Size
1000984958 XS ---Different ID different size (ID count 1)
1000972994 S ---Same ID different size (ID count 2)
1000972994 L ---Same ID different size (ID count 2)

Result:

ID Size Count
1000984958 XS 1
1000972994 S 2
1000972994 L 2

如何在ASP.NET C#中获取具有重复值的重复计数?

答案1

得分: 0

要计算集合中重复项的数量,您可以使用LINQ。例如,在您的情况下,您可以根据ID对列表进行分组,并计算该列表中的元素数量。

例如:

var q = from x in tb
        group x by x.ID into g
        let count = g.Count()
        orderby count descending
        select new { Value = g.Key, Count = count };

或者

var q = tb.GroupBy(x => x.ID)
           .Select(g => new { Value = g.Key, Count = g.Count() })
           .OrderByDescending(x => x.Count);

下面是一个工作的控制台应用程序示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class Program
    {
        public static void Main()
        {
            List<MyClass> tb = new List<MyClass>
            {
                new MyClass {ID =1000984958, Size = "XS", IdCount = 1 },
                new MyClass {ID = 1000972994, Size = "S", IdCount = 2},
                new MyClass {ID= 1000972994, Size = "L", IdCount =  2 },
                new MyClass {ID= 1000991726, Size = "S", IdCount =  3 },
                new MyClass {ID= 1000991726, Size = "M", IdCount =  3 },
                new MyClass {ID= 1000991726, Size = "L", IdCount =  3 },
                new MyClass {ID= 1000980092, Size = "M", IdCount =  1 },
            };

            var q = from x in tb
                    group x by x.ID into g
                    let count = g.Count()
                    orderby count descending
                    select new { Value = g.Key, Count = count };

            foreach (var x in q)
            {
                Console.WriteLine("Value: " + x.Value + " Count: " + x.Count);
            }
        }
    }
    public class MyClass
    {
        public int ID { get; set; }
        public string Size { get; set; }
        public int IdCount { get; set; }
    }
}

如何在ASP.NET C#中获取具有重复值的重复计数?

英文:

To count the duplicate items in your collection you can use LINQ. For example in your case you can group you list by ID and count the number of element in this list.

For example :

var q = from x in tb
		group x by x.ID into g
		let count = g.Count()
		orderby count descending
		select new { Value = g.Key, Count = count };

OR

var q = tb.GroupBy(x =&gt; x.ID)
            .Select(g =&gt; new { Value = g.Key, Count = g.Count() })
            .OrderByDescending(x =&gt; x.Count);

Working console application is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class Program
    {
        public static void Main()
        {
            List&lt;MyClass&gt; tb = new List&lt;MyClass&gt;
            {
                new MyClass {ID =1000984958, Size = &quot;XS&quot;, IdCount = 1 },
                new MyClass {ID = 1000972994, Size = &quot;S&quot;, IdCount = 2},
                new MyClass {ID= 1000972994, Size = &quot;L&quot;, IdCount =  2 },
                new MyClass {ID= 1000991726, Size = &quot;S&quot;, IdCount =  3 },
                new MyClass {ID= 1000991726, Size = &quot;M&quot;, IdCount =  3 },
                new MyClass {ID= 1000991726, Size = &quot;L&quot;, IdCount =  3 },
                new MyClass {ID= 1000980092, Size = &quot;M&quot;, IdCount =  1 },
            };

            var q = from x in tb
                    group x by x.ID into g
                    let count = g.Count()
                    orderby count descending
                    select new { Value = g.Key, Count = count };

            foreach (var x in q)
            {
                Console.WriteLine(&quot;Value: &quot; + x.Value + &quot; Count: &quot; + x.Count);
            }
        }
    }
    public class MyClass
    {
        public int ID { get; set; }
        public string Size { get; set; }
        public int IdCount { get; set; }
    }
}

如何在ASP.NET C#中获取具有重复值的重复计数?

huangapple
  • 本文由 发表于 2023年7月4日 23:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定