本文介绍了Sum variuos实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我从模型中提取不同的实体。用for循环来做这个,比如

for(i = 0; i< whatever; ++ i)

{

var myEntity = GetMyEntity ();

myEntityList + = myEntity +\ n;

}

结果列表(myEntityList)将如下所示



MyEntity1

MyEntity1

MyEntity3

MyEntity4

MyEntity3

MyEntity1

MyEntity3

MyEntity4

MyEntity4

MyEntity4

MyEntity3



我的问题不是。我如何安排和总结看起来像

MyEntity1 = 3

MyEntity3 = 4

MyEntity4 = 4



我根本没有编程技能。



谢谢



我尝试过:



创建数组。可能是一个更聪明的方式来处理它。如上所述,我没有编程技巧

HiI am extracting different entities from a model. Doing this with for loop, like
for(i=0; i<whatever; ++i)
{
var myEntity = GetMyEntity();
myEntityList += myEntity + "\n";
}
Result list (myEntityList) will look like this

MyEntity1
MyEntity1
MyEntity3
MyEntity4
MyEntity3
MyEntity1
MyEntity3
MyEntity4
MyEntity4
MyEntity4
MyEntity3

No my question is. How can I arrange and sum this to look like
MyEntity1 = 3
MyEntity3 = 4
MyEntity4 = 4

I have no programming skills at all.

Thanks

What I have tried:

Creating an Array. Could be a smarter way to deal with it. As mentioned, I have no programming skills

推荐答案

// This would be your list of (myEntityList)
            var myEntitity = new List<string>();
            myEntitity.Add("MyEntity1");
            myEntitity.Add("MyEntity1");
            myEntitity.Add("MyEntity3");
            myEntitity.Add("MyEntity4");
            myEntitity.Add("MyEntity3");
            myEntitity.Add("MyEntity1");
            myEntitity.Add("MyEntity3");
            myEntitity.Add("MyEntity4");
            myEntitity.Add("MyEntity4");
            myEntitity.Add("MyEntity4");
            myEntitity.Add("MyEntity3");


            var results = myEntitity.GroupBy(m => m).Select(m => new {EntityName = m.Key, Count = m.Count()});

            foreach (var result in results)
            {
                Console.WriteLine("{0}: {1}", result.EntityName, result.Count);
            }





哪个提供输出





Which provides an output of

MyEntity1: 3
MyEntity3: 4
MyEntity4: 4


这篇关于Sum variuos实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 06:42