How to use ILookup<>

Represents a collection of keys each mapped to one or more values.

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            //start
            ILookup<string, DataRow> lookUpList = stu.getDt().AsEnumerable().ToLookup(x => x.Field<string>("stuName"));
            foreach (var VARIABLE in lookUpList)
            {
                Console.Write("key = "+VARIABLE.Key);
                Console.WriteLine("   ");
                Console.Write("values = ");
                var v = lookUpList[VARIABLE.Key]; //That only to appear in this variable's type of <IEnumerable<DataRow>>!
                foreach (var dr in lookUpList[VARIABLE.Key])
                {
                    Console.WriteLine();
                    Console.WriteLine("dr0=" + dr["stuName"]);
                    Console.WriteLine("dr1=" + dr["semester"]);
                    Console.WriteLine("dr2=" + dr["mathGrade"]);
                    Console.WriteLine("dr3=" + dr["artGrade"]);
                    Console.WriteLine();
                }
            }
        }
    }

    class Student
    {
        public string stuName { get; set; }
        public int? mathGrade { get; set; } 
        public int? artGrade { get; set; }

        public DataTable getDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("stuName", typeof(string));
            dt.Columns.Add("semester", typeof(int));
            dt.Columns.Add("mathGrade", typeof(int));
            dt.Columns.Add("artGrade", typeof(int));
            DataRow dr1 = dt.NewRow();
            dr1["stuName"] = "张三";
            dr1["semester"] = 1;
            dr1["mathGrade"] = 99;
            dr1["artGrade"] = 99;
            dt.Rows.Add(dr1);
            DataRow dr3 = dt.NewRow();
            dr3["stuName"] = "张三";
            dr3["semester"] = 2;
            dr3["mathGrade"] = 98;
            dr3["artGrade"] = 98;
            dt.Rows.Add(dr3);
            DataRow dr2 = dt.NewRow();
            dr2["stuName"] = "李四";
            dr2["semester"] = 1;
            dr2["mathGrade"] = 98;
            dr2["artGrade"] = 98;
            dt.Rows.Add(dr2);
            return dt;
        }
    }

}

How to use ILookup<>

How to use ILookup<>

上一篇:无参数名post数据客户端以及接口是如何实现的?


下一篇:使用TypeScript校验运行时数据