1. 程式人生 > >Entity Framework(一)

Entity Framework(一)

重復 取數 \n wan 條件 數據 對象屬性 方法 升序

相關知識點復習:

1、var 類型推斷: var p=new Person();

2、匿名類型: var a=new {Name="wang",Age=12 };

3、給新創建的對象屬性賦值的簡化方法: var P=new Person{Name="wang" ,Age=12 } 等價於 Person p=new Person() ; p.Name="wang"; p.Age=12;

4、.NET高級擴展方法:

            var s0 = new Person { Name = "wang", Age = 12
, Salary = 1200 }; var s1 = new Person { Name = "chen", Age = 13, Salary = 2400 }; var s2 = new Person { Name = "li", Age = 16, Salary = 2300 }; var s3 = new Person { Name = "qian", Age = 18, Salary = 1500 }; var s4 = new Person { Name = "liu", Age = 21, Salary = 2100
}; var s5 = new Person { Name = "zhao", Age = 23, Salary = 3211 }; var s6 = new Person { Name = "sun", Age = 27, Salary = 2390 }; List<Person> ps = new List<Person>(); ps.Add(s0); ps.Add(s2); ps.Add(s3); ps.Add(s6);
var teacher1 = new Teacher() { Name = "wang",Students = {s0,s1,s2 } }; var teacher2 = new Teacher() { Name = "li" , Students = { s3, s4, s5 } }; Teacher[] ts = { teacher1, teacher2 };

(1)Any() ,判斷集合是否包含元素,返回值是bool,一般比Count()>0效率高,Any還可以指定條件表達式。bool b=list.Any(p=>p.Age>50) 等價於bool b=list.Where(p=>=.Age>50).Any();

Console.WriteLine(ps.Any(p=>p.Salary>2000));

(2) Distinct(),剔除完全重復的數據。(*)註意自定義對象的Equal問題,需要重寫Equal和GetHashCode方法來進行內容比較

(3)排序: 升序 list.OrderBy(p=>p.Age); 降序 list.OrderByDiscending(p=>p.Age) . 指定多個排序規則,而不是多個OrderBy,而是:list.OrderByDiscending(p=>p.Age).ThenBy(p=>p.Salary),也支持ThenByDiscending()

            var p1 = ps.OrderBy(p => p.Salary);
            foreach (var p in p1)
            {
                Console.WriteLine("姓名:"+p.Name+"\n"+"薪水"+p.Salary);
            }

(4) skip(n)跳過前n條數據;Take(n)獲取最多n條數據,如果不足也不會報錯,常用來分頁獲取數據。 List.Skip(3).Take(2) 跳過前3條數據獲取2條數據

            var p1 = ps.Skip(2).Take(3);
            foreach (var p in p1)
            {
                Console.WriteLine("姓名:"+p.Name+"\n"+"薪水"+p.Salary);
            }

(5)Except(item1) 排除當前集合中在item1中存在的元素;

            int[] num1 = { 3, 5, 8, 11, 15 };
            int[] num2 = { 5, 8, 9,  15 };
            var num3 = num1.Except(num2);

            foreach (var p in num3)
            {
                Console.WriteLine(p);
            }

(6)Union(item1) 把當前集合和item1中組合;

            int[] num1 = { 3, 5, 8, 11, 15 };
            int[] num2 = { 5, 8, 9,  15 };
            var num3 = num1.Union(num2);

            foreach (var p in num3)
            {
                Console.WriteLine(p);
            }

(7)Intersect(item1)把當前集合和item1中取交集

            int[] num1 = { 3, 5, 8, 11, 15 };
            int[] num2 = { 5, 8, 9,  15 };
            var num3 = num1.Intersect(num2);

            foreach (var p in num3)
            {
                Console.WriteLine(p);
            }

(8) 分組:

            foreach (var p in ps.GroupBy(g1=>g1.Gender))
            {
                Console.WriteLine(p.Key);
                foreach (var p2 in p)
                {
                    Console.WriteLine(p2);
                }
            }

            Console.ReadKey();

(9)SelectMany: 把集合中的每個對象的另外集合屬性的值重新拼接為一個新的集合;

            foreach (var s in ts.SelectMany(t=>t.Students))
            {
                Console.WriteLine(s);
                
            }

            Console.ReadKey();

Entity Framework(一)