---恢复内容开始---
1.对查询结果进行转换。
string[] names={"ADE","Jerry","Steves"}; var ns=from s in names where s.StartWiths("A") Select s.ToLower(); foreare(var o in na){ Console.WriteLine(o); }
2. 对于数组,select可以对数组元素以及索引进行操作:
string[] names = { "Jack", "Bob", "Bill", "Catty", "Willam" }; var rs = names.Select((name, index) => new { Name = name, Index = index }); foreach (var r in rs) Console.WriteLine(r);
3. 对于集合(wpf中的mvvm为例子),查询选中项是否符合要求:
private const string LMPPropertyName="LMPSource"; private OberservableCollection<YM_LMP_SQB> _LMPSource; public OberservableCollection<YM_LMP_SQB> LMPSource{ get{return _LMPSource;} set{ if(_LMPSource==value) return; RaisePropertyChanging(LMPPropertyName); _LMPSource==value; RaisePropertyChanged(LMPPropertyName); } } int a=LMPSource.Select( (s,index)=>new{s,index}).FirstOrDefault(w=w.s==LMPSource.Where(e=>e.ZT!="0").FirstOrDefault()).index+1; Console.WriteLine(a)
4. 组合查询,可以对多个数据源进行组合条件查询(相当于使用SelectMany函数),下面的例子其实就相对于一个双重循环遍历:
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var pairs = from a in numbersA, b in numbersB where a < b select new {a, b}; Console.WriteLine("Pairs where a < b:"); foreach (var pair in pairs) Console.WriteLine("{0} is less than {1}", pair.a, pair.b);
---恢复内容结束---