linq復習

9998 ワード

/// <summary>
        /// join     
        /// </summary>
        public void Test6()
        {
            var namelist = new[] { 
                new{id = "1", name = "Jon"},
                new{id = "2", name = "Holly"},
                new{id = "1", name = "Tom"},
                new{id = "3", name = "Tom"},
                new{id = "3", name = "Tom"}
            };
            var dtlist = new[] { 
                new{id = "2", age = 23, },
                new{id = "2", age = 28, },
                new{id = "3", age = 26},
                new{id = "1", age = 25}
            };
            var query = from n in namelist
                        join d in dtlist
                        on n.id equals d.id into tempdt
                        select new
                        {

                        };
            //...
        }

        /// <summary>
        ///      
        /// </summary>
        public void Test8()
        {
            //       ,     ,    
            //var q = from e1 in db.Employees
            //        from e2 in e1.Employees
            //        where e1.City == e2.City
            //        select new
            //        {
            //            name1 = e1.FirstName + e1.LastName,
            //            name2 = e2.FirstName + e2.LastName,
            //            e1.City
            //        };
        }
        /// <summary>
        ///    =>     (Two way join)
        /// </summary>
        /// 
        public void Test9()
        {
            //var q = from c in db.Customers
            //        join o in db.Orders on c.CustomerID
            //        equals o.CustomerID into orders
            //        select new
            //        {
            //            c.ContactName,
            //            OrderCount = orders.Count()
            //        };
            /*       ,   1,      c(from c in db.Customers),
             *    Many,       o ( join o in db.Orders ),
             *         c,     o,    o,   orders,    ,
             *      o   orders,   into  。        select   ,orders        Count。
             */
        }

        /// <summary>
        /// join      
        /// </summary>
        public void Test5()
        {
            var str = new[] { 
                new{id = "1", name = "Jon"},
                new{id = "2", name = "Holly"},
                new{id = "3", name = "Tom"}
            };
            var str2 = new[] { 
                new{id = "2", age = 23},
                new{id = "3", age = 26},
                new{id = "1", age = 25}
            };
            var query = from n in str
                        join a in str2
                        on n.id equals a.id into tempa
                        from ta in tempa.DefaultIfEmpty()
                        select new
                        {
                            n,
                            ta
                        };
            foreach (var item in query)
            {
                Response.Write(string.Format("name:{0}, age:{1}<br />", item.n.name, item.ta.age.ToString()));
                /*
                    name:Jon, age:25
                    name:Holly, age:23
                    name:Tom, age:26
                 */
            }

            //            
            //var categories = from p in db.Products
            //                 group p by p.CategoryID into g
            //                 select new
            //                 {
            //                     CategoryID = g.Key,
            //                     CheapestProducts = from p2 in g
            //                                        where p2.UnitPrice == g.Min(x => x.UnitPrice)
            //                                        //                   
            //                                        //where p2.UnitPrice == g.Average(x = x.UnitPrice) 
            //                                        select p2
            //                 };
        }

        /// <summary>
        /// join、     
        /// </summary>
        public void Test4()
        {
            //      
            // var str = new { id = "1", name = "jon" };

            //        
            var str = new[] { 
                new{id = "1", name = "Jon"},
                new{id = "2", name = "Holly"},
                new{id = "3", name = "Tom"}
            };
            var str2 = new[] { 
                new{id = "1", name = "Robin"},
                new{id = "2", name = "William"},
                new{id = "2", name = "kkk"},
                new{id = "3", name = "jjjj"}
            };

            //     
            //var query = from a in str
            //            join b in str2 on a.id equals b.id
            //            select new { Aname = a.name, Bname = b.name };
            //foreach (var item in query)
            //{
            //    Response.Write(string.Format("Aname:{0}, Bname:{1}", item.Aname, item.Bname) + "<br />");
            //    /*
            //        Aname:Jon, Bname:Robin
            //        Aname:Holly, Bname:William
            //        Aname:Holly, Bname:kkk
            //        Aname:Tom, Bname:jjjj
            //     */
            //}
            var query = from a in str
                        join b in str2 on a.id equals b.id into temp2
                        select new
                        {
                            a,
                            temp2
                        };
            foreach (var item in query)
            {
                Response.Write(string.Format("Aname:{0}, =>Bname Count:{1}", item.a.name, item.temp2.Count()) + "<br />");
                /*
                    Aname:Jon, =>Bname Count:1
                    Aname:Holly, =>Bname Count:2
                    Aname:Tom, =>Bname Count:1
                 */
            }
        }

        /// <summary>
        /// group by  
        /// </summary>
        public void Test3()
        {
            char[] gitem = { 'a', 'b', 'c', 'f' }; //         
            string[] str = { "aa", "bb", "cc", "dd", "fine" };

            //var query = from s in str
            //            group s by s[0] into ws //      s, s[0]  
            //            where gitem.Contains(ws.Key)
            //            orderby ws.Key ascending
            //            select ws.Key;
            var q = from s in str
                    where gitem.Contains(s[0])
                    select s;

            //foreach (var item in query)
            foreach (var item in q)
            {
                //Response.Write(string.Format("{0},<br />", item));
                /*
                   a,
                   b,
                   c,
                   f,
                */
                Response.Write(string.Format("{0},<br />", item));

            }
        }

        /// <summary>
        /// let  
        /// </summary>
        public void Test2()
        {
            string[] strings = { "A penny saved is a penny earned.", "The aaxly sdj", "the pa is no" };
            //        a e   ,      
            var g = from j in strings
                    let w = j.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    from k in w
                    let nw = k.ToLower()
                    where nw[0] == 'a' || nw[0] == 'e'
                    select k;

            foreach (var item in g)
            {
                Response.Write(string.Format("{0},<br />", item));
            }
        }

        /// <summary>
        ///    
        /// </summary>
        public void Test1()
        {
            List<Student> students = new List<Student>         
            {            
                new Student 
                {
                    LastName="xiaogui",Scores=new List<int>{97,42,91,60}
                },
                new Student
                {
                    LastName="xiaozhan",Scores=new List<int>{50,92,81,60}
                },   
                new Student 
                {
                    LastName="xiaolan",Scores=new List<int>{32,32,81,90}
                },
                new Student 
                {
                    LastName="xiaowan",Scores=new List<int>{92,22,81,60}
                },         
            };

            //       90   
            var q = from s in students
                    from o in s.Scores
                    where o > 90
                    select new
                    {
                        lastname = s.LastName,
                        o
                    };
            foreach (var item in q)
            {
                Response.Write(string.Format("{0}:{1}<br />", item.lastname, item.o));
                /*Note:xiaogui   2 
                 * 
                    xiaogui:97
                    xiaogui:91
                    xiaozhan:92
                    xiaowan:92
                 */
            }
        }

    }//testlinq

    public class Student
    {
        public string LastName { get; set; }
        public List<int> Scores { get; set; }
    }
    public class Employee
    {
        public string First { get; set; }
        public string Last { get; set; }
        public int ID { get; set; }
    }