LINQ之Lambda
最近一年一直没怎么跟进新技术,慢慢人都快成古董了。
今天才下了vs2008的beta2,研究了半天才在查资料的时候知道早就出了正式版了。真正做了次火星人。
发现LINQ的时候才眼前一亮。
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from num in numbers
where num < 5
select num;
类似sql的集合操作方式不得不让人兴奋。这在c#3.0中叫做Lambda。
除此之外还有另外一种写法:
var lowNums = numbers.Where(i => i < 5).Select(i => i);
下面看看query keywords吧。
from:变量定义,只是跟from这个单词感觉不怎么靠谱。
第二种写法中的where后面的第一个i表达的也是这个概念。
where:查询条件,这跟sql还是一样的。
第二种写法“=>”之后的部分。
select:返回值,一般是返回from定义的变量,并可对该变量进行运算。比如select num+1.
group:分组。
string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" };
var wordGroups =
from w in words
group w by w[0]; 这段代码中 wordGroups就是查询的结果,一个由IGrouping组成的IEnumerable集合。IEnumerable的第一项IGrouping集合中包含着“blueberry”与“banana”。
第二种写法
var wordGroups = words.GroupBy(i => i[0]);
into:插入,类似与sql中的创建临时表的操作。
看代码
var wordGroups1 =
from w in words
group w by w[0] into newGroup
where newGroup.Count() >= 2
select new { FirstLetter = newGroup.Key, Words = newGroup.Count() };
这句话的意思是说将words中的单词通过首字母分组(与group关键字的演示代码相同)后放入newGroup,也就是一个IEnumerable集 合,然后再根据newGroup的Count属性进行筛选。并将筛选集存入一个由新建立的对象组成的集合。而这个对象有两个属性分别是 FirstLetter、Words。自然这俩属性值的来源正式newGroup的key与count属性。
orderby :排序
IEnumerable<string> sortedWords =
from w in words
orderby w ascending //或descending
select w; join:像是数据库里的外键关联。
var query =
from c in categories
join p in products on c equals p.Category
select new { Category = c, p.Name };
这句话其实就是返回products中的category属性值在categories字典集合中存在对象,然后将对象的Categpry与Name属性构成的新对象的集合并赋于query。
let:存储临时值。
var query =
from sentence in strings
let words = sentence.Split(' ')
from word in words
let w = word.ToLower()
where w[0] == 'a' || w[0] == 'e'
|| w[0] == 'i' || w[0] == 'o'
|| w[0] == 'u'
select word;
这个就像两个嵌套的for循环,类似于下面代码的实现。
List<string> resualt = new List<string>();
foreach (string sentence in strings)
{
string[] words = sentence.Split(' ');
foreach (string word in words)
{
if (word.ToLower() == 'a' || word.ToLower() == 'e' || word.ToLower() == 'i'
|| word.ToLower() == 'o' || word.ToLower() == 'u')
{
resualt.Add(word);
}
}
}
转载:http://www.cnblogs.com/tianyamoon/articles/1027544.html
今天才下了vs2008的beta2,研究了半天才在查资料的时候知道早就出了正式版了。真正做了次火星人。
发现LINQ的时候才眼前一亮。
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from num in numbers
where num < 5
select num;
除此之外还有另外一种写法:
var lowNums = numbers.Where(i => i < 5).Select(i => i);下面看看query keywords吧。
from:变量定义,只是跟from这个单词感觉不怎么靠谱。
第二种写法中的where后面的第一个i表达的也是这个概念。
where:查询条件,这跟sql还是一样的。
第二种写法“=>”之后的部分。
select:返回值,一般是返回from定义的变量,并可对该变量进行运算。比如select num+1.
group:分组。
string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" };
var wordGroups =
from w in words
group w by w[0];第二种写法
var wordGroups = words.GroupBy(i => i[0]);into:插入,类似与sql中的创建临时表的操作。
看代码
var wordGroups1 =
from w in words
group w by w[0] into newGroup
where newGroup.Count() >= 2
select new { FirstLetter = newGroup.Key, Words = newGroup.Count() };
orderby :排序
IEnumerable<string> sortedWords =
from w in words
orderby w ascending //或descending
select w;
var query =
from c in categories
join p in products on c equals p.Category
select new { Category = c, p.Name };
let:存储临时值。
var query =
from sentence in strings
let words = sentence.Split(' ')
from word in words
let w = word.ToLower()
where w[0] == 'a' || w[0] == 'e'
|| w[0] == 'i' || w[0] == 'o'
|| w[0] == 'u'
select word;
List<string> resualt = new List<string>();
foreach (string sentence in strings)
{
string[] words = sentence.Split(' ');
foreach (string word in words)
{
if (word.ToLower() == 'a' || word.ToLower() == 'e' || word.ToLower() == 'i'
|| word.ToLower() == 'o' || word.ToLower() == 'u')
{
resualt.Add(word);
}
}
}转载:http://www.cnblogs.com/tianyamoon/articles/1027544.html
————————————————————————————————————————
一个人的时候,总是在想
我的生活到底在期待什么……



浙公网安备 33010602011771号