英文:
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 |
答案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; }
}
}
英文:
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 => x.ID)
.Select(g => new { Value = g.Key, Count = g.Count() })
.OrderByDescending(x => 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<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; }
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论