C# 计算对象c列表中2个属性的出现次数

C# 计算对象c列表中2个属性的出现次数,c#,list,object,C#,List,Object,我有两个对象列表 List<Student> studentList = new List<Student>() { new Student(){ StudentName="Bill", MobileOS = "Android"}, new Student(){ StudentName="Bill", MobileOS = "Android"} new Student(){ StudentName="Steve",Mobil

我有两个对象列表

   List<Student> studentList = new List<Student>() { 


      new Student(){ StudentName="Bill", MobileOS = "Android"},
      new Student(){ StudentName="Bill", MobileOS = "Android"}
      new Student(){ StudentName="Steve",MobileOS = "IOS"},
      new Student(){ StudentName="Ram",  MobileOS = "IOS"},
      new Student(){ StudentName="Bill", MobileOS = "IOS"}
}
在我的另一个列表中,我想存储过滤后的数据。范例

    List<FilteredData> filteredData= new List<FilteredData>(){

          new FilteredData(){ StudentName="Bill", Count=3, Android = 2, Ios = 1},
          new FilteredData(){ StudentName="Steve",Count=1, Android = 0, Ios = 1},
          new FilteredData(){ StudentName="Ram",  Count=1, Android = 0, Ios = 1},
}
注意:计数值基于相同StudentName的出现 Android和Ios的数量是基于MobileOS的

那么,我如何计算相同的学生姓名并将其存储到新列表中?
我搜索了一整天,但似乎找不到解决方案

您可以使用LINQ对结果进行分组:

List<FilteredData> filteredData = studentList
    .GroupBy(x => x.StudentName)
    .Select(x => new FilteredData()
    {
        StudentName = x.Key,
        Count = x.Count(),
        Android = x.Count(y => y.MobileOS == "Android"),
        Ios = x.Count(y => y.MobileOS == "Ios")
    }).ToList();
请注意,此解决方案将每个组迭代三次,但这不应该是问题,除非studentList中的项目数量很大。然后,最好手动遍历每个组x,并在Select func中跟踪三个单独变量中的数字。

尝试以下操作:

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


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> studentList = new List<Student>() { 
                new Student(){ StudentName="Bill", MobileOS = "Android"},
                new Student(){ StudentName="Bill", MobileOS = "Android"},
                new Student(){ StudentName="Steve",MobileOS = "IOS"},
                new Student(){ StudentName="Ram",  MobileOS = "IOS"},
                new Student(){ StudentName="Bill", MobileOS = "IOS"}
            };

            List<FilteredData> filteredData = studentList
                .GroupBy(x => x.StudentName)
                .Select(x => new FilteredData() {
                    StudentName = x.Key,
                    Count = x.Count(),
                    Android = x.Where(y => y.MobileOS == "Android").Count(),
                    Ios = x.Where(y => y.MobileOS == "IOS").Count()
                }).ToList();
        }
    }
        public class Student
        {
            public string StudentName { get;set;}
            public string MobileOS { get;set;}

        }
        public class FilteredData
        {
            public string StudentName { get;set;}
            public int Count { get;set;}
            public int Android { get;set;}
            public int Ios { get;set;}
        }


}

@mm8基本上过滤第一个列表并将其存储到第二个列表中list@mm8或者你知道如何实现吗?谢谢,但是你的代码有一些错误,应该用另一个替换x中的x.Mobilevariable@StackOverFlowSavedMyLife:改为y。我看不出这个答案和我的答案有什么区别,除了你先调用Where,然后调用Count,这会导致不必要的额外迭代。您应该使用接受谓词的Count重载。注释表示您的代码有错误。我的课程已经完成了。