EF GroupBy 分组 取某条的 总数

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

namespace hashmap
{
    class A
    {
        public string name { get; set; }
        public string age { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("");
            get();
            Console.WriteLine();
            Dictionary<int, string> td = new Dictionary<int, string>();
            td[5] = "5";
            Console.WriteLine();
            //KeyValuePair<>



            List<A> all = new List<A>(){
                new A{name="1000A",age="1"},
                new A{name="1000A",age="2"},
                new A{name="1000A",age="3"},
                new A{name="1000A",age="4"},
                new A{name="1000B",age="4"},
                new A{name="1000B",age="4"},
                new A{name="1000B",age="4"},
            };
            Console.WriteLine("---");
            var allgrbname = all.GroupBy(a => a.name);       // allgrbname  就是一个集合     集合成员  存在 key 与 value(集合 集合成员为A)
            //在此例中 allgrbname有两个元素 分别为     key 1000A value 长 4       key 1000B value 长 3
            Console.WriteLine(allgrbname.Count(a => a.Key == "1000A"));    //1  Count 返回满足集合条件行数  Key为1000A 的  1条
            Console.WriteLine(allgrbname.Where(a => a.Key == "1000A").Count());     //1    Where 过滤集合元素  Key为1000A 的 返回集合  返回行数 1条
            Console.WriteLine(allgrbname.Where(a => a.Key == "1000A").FirstOrDefault().Count());    //4    Where 过滤集合元素  Key为1000A 的  返回集合 元素1条  返回集合中第一个元素    key 1000A value 长 4        返回 集合 allgrbname成员的value
            Console.WriteLine(allgrbname.Where(a => a.Key == "1000A").FirstOrDefault().Key);      //1000A   返回 集合 allgrbname成员的key



            var sdsds = all.Where(a => a.name == "asdasd");
            Console.WriteLine(sdsds.Count());
        }

        static void get()
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
        }
    }
}

 

posted @ 2018-12-05 18:00  ~雨落忧伤~  阅读(619)  评论(0编辑  收藏  举报