LINQ小记

 //LINQ 根据长度查询出来
            string[] strs = new string[] { "1", "22", "333", "4444", "55555" };            
            var end = from str in strs where str.Length < 3 select str;
            var i = "";
            foreach (var item in end)
            {
                i = i + item + "|";
            }
            this.Label1.Text = i.Substring(0, i.Length - 1);


//LINQ 根据长度分组进行查询
            string[] words = new string[] { "hello", "cup", "vagetable", "mouse", "telephone" };
            var list =
                from w in words
                group w by w.Length into lists
                orderby lists.Key ascending
                select new { key = lists.Key, value = lists };
            foreach (var item in list)
            {
                Label2.Text += string.Format("长度:{0},{1}</br>", item.key, string.Join(",", item.value));
            }


 //LINQ 根据同名进行查询
            string[] ss = new string[]
            { "hello", "hello", "cup", "cup", "cup", "vagetable", "mouse", "telephone", "telephone" };
            var l =
                from s in ss
                group s by s into lists
                orderby lists.Key
                select lists;
            foreach (var item in l)
            {
                Label3.Text += string.Format("{0},数量:{1}<br/>",item.FirstOrDefault(),item.Count().ToString());
            }

原文地址:https://www.cnblogs.com/dabexiong/p/5223042.html