开始写博客,学习Linq(5)
开始写代码了,我会把自己的代码粘贴在这里,好不容易可以实践了,可是不能偷懒的。
string[] words = { "hello", "wonderful", "linq", "beautiful", "world" }; var shortWords = from word in words where word.Length <= 5 select word; foreach (var word in shortWords) { Console.WriteLine(word); } Console.Read();
这是个简单的运用Linq来操作集合的。简单的是好理解,但让你理解的不深入,所以,不要害怕困难。
string[] words = { "hello", "wonderful", "linq", "beautiful", "world" }; var groups = from word in words orderby word ascending group word by word.Length into lengthGroups orderby lengthGroups.Key descending select new { Length = lengthGroups.Key, Words = lengthGroups }; foreach (var group in groups) { Console.WriteLine("Words of length "+group.Length); foreach (var word in group.Words) { Console.WriteLine(" "+word); } } Console.Read();