LINQ 101——约束、投影、排序

什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询、集合以及转换操作。它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能的类库。

什么是LINQ 101:是学习LINQ的不错的资源(下载地址:http://pan.baidu.com/s/1ntE74NJ),当初我也是看LINQ101来熟悉LINQ的。现把我当时的学习笔记整理出来

Restriction (约束)
Projection (投影)
Ordering (排序)
Partitioning (分区)
Join
Aggregate(聚合)
Grouping(分组)
Set(集合)
Conversion(转换)
Element(元素操作)

本篇文章介绍前三个

一、 约束 ——where

var query = from item in [集合] where [item满足的条件] select item;

int[] numbers = { , , , , , - };
var query = from n in numbers
where n % ==
select n;
Console.WriteLine("被3整除的数:");
foreach (var num in numbers)
{
Console.WriteLine(num);
}

多个Where 条件且筛选

 var query = from item in sources
where (字段1的条件1) || (字段1的条件2)
where (字段2的条件1) || (字段2的条件2)
select new Entity .... 如
var query = from item in sources
where (string.IsNullOrEmpty(txtSearch.Text.Trim())) || (item.Filed1.Contains(txtSearch.Text.Trim()))
where (string.IsNullOrEmpty(txtFilter.Text.Trim())) || (item.Filed2.Contains(txtFilter.Text.Trim()))
...
select new Entity ....

二、投影——select

例1:让数组中每个数都加1

 int[] numbers = { , , , , , ,- };

 var numsPlusOne =
from n in numbers
select n + ; Console.WriteLine("整数+1:");
foreach (var num in numsPlusOne)
{
Console.WriteLine(num);
}

例2:选取某类中一个属性

 public class Config
{
public Guid ID { get; set; }
public string Name { get; set; }
public string Remark { get; set; } Config() { } public static List<Config> GetConfigs()
{
return new List<Config>
{
new Config{ ID=Guid.NewGuid(), Name="XX1号",Remark="Remark1号"},
new Config{ ID=Guid.NewGuid(), Name="XX2号",Remark="Remark2号"},
new Config{ ID=Guid.NewGuid(), Name="XX3号",Remark="Remark3号"}
};
}
} static void Linq3()
{
var source = Config.GetConfigs();
var query = from item in source
select item.Name;
foreach (var c in query)
{
Console.WriteLine(c);
}
}

例3:数组下标转换

 static void Linq4()
{
int[] numbers = { , , , , , , , , , };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var textNums =
from n in numbers
select strings[n]; Console.WriteLine("数组下标转换");
foreach (var s in textNums)
{
Console.WriteLine(s);
}
}

例3:匿名类型,大小写

 static void Linq5()
{
string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };
var query = from word in words
select new { Upper = word.ToUpper(), Lower = word.ToLower() };
foreach (var item in query)
{
Console.WriteLine("大写: {0} 小写: {1}",item.Upper,item.Lower);
}
}

例4:将投影的属性放到一个类中

 public class Dto
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
} static void Linq6()
{
var source = Config.GetConfigs();
var query = from item in source
select new Dto { Field1 = item.ID.ToString(), Field2 = item.Name, Field3 = item.Remark };
foreach (var item in query)
{
Console.WriteLine("{0} {1} {2}", item.Field1, item.Field2, item.Field3);
}
}

例5:筛选 加where条件参见 where 例1

例6:复合from子句(选取座机号码区号为021的客户信息)

 public class Guest
{
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; } /// <summary>
/// 年龄
/// </summary>
public int Age { get; set; } /// <summary>
/// 电话表
/// </summary>
public List<string> TelTable { get; set; }
} static void Linq8()
{
var source = new List<Guest>
{
new Guest{ Name="张三",Age=,TelTable=new List<string>{"010-123456",""}},
new Guest{ Name="李四",Age=,TelTable=new List<string>{"021-123456",""}},
new Guest{ Name="王五",Age=,TelTable=new List<string>{"022-123456",""}}
};
// 找电话簿中是021区号的客户
var query = from guest in source
from tel in guest.TelTable
where tel.Contains("021-")
select guest; foreach (var guest in query)
{
Console.WriteLine("{0} 年龄:{1}", guest.Name, guest.Age);
foreach (var tel in guest.TelTable) { Console.WriteLine(" 电话:{0}",tel); }
}
}

例7:多个 form 子句(交叉连接)

 static void Linq7()
{
int[] numbersA = { , , , , , , };
int[] numbersB = { , , , , }; var pairs =
from a in numbersA
from b in numbersB
where a < b
select new { a, b }; Console.WriteLine("交叉连接 a < b:");
foreach (var pair in pairs)
{
Console.WriteLine("{0} 小于 {1}", pair.a, pair.b);
}
}

三、排序

orderby 排序字段

orderby 排序字段 descending

Thenby(orderby 多个排序字段)

ThenByDescending  (orderby 多个排序字段 descending)

例1:orderby 排序字段

 static void Linq9()
{
string[] words = { "cherry", "apple", "blueberry" }; var sortedWords =
from w in words
orderby w
select w; Console.WriteLine("单词排序 order by 单个排序字段:");
foreach (var w in sortedWords)
{
Console.WriteLine(w);
}
}

例2:orderby 排序字段 descending

 static void Linq10()
{
string[] words = { "cherry", "apple", "blueberry" }; var sortedWords =
from w in words
orderby w descending
select w; Console.WriteLine("单词排序 order by 单个排序字段 从大到小:");
foreach (var w in sortedWords)
{
Console.WriteLine(w);
}
}

例3:Thenby(orderby 多个排序字段)

 static void Linq11()
{
string[] words = { "cherry", "appppp", "cherryo", "appppppple", "blueberry" }; // 先按名称排,再按长度排序
var sortedWords =
from w in words
orderby w,w.Length
select w; Console.WriteLine("单词排序 ThenBy 多个排序字段");
foreach (var w in sortedWords)
{
Console.WriteLine(w);
}
}

例4:ThenByDescending  (orderby 多个排序字段 descending)

注意主要条件和次要的升降序不传递

 static void Linq12()
{
string[] words = { "cherry", "appppp", "cherryo", "appppppple", "blueberry" }; // 先按名称排,再按长度排序
var sortedWords =
from w in words
orderby w descending, w.Length descending
select w; Console.WriteLine("单词排序 ThenBy 多个排序字段");
foreach (var w in sortedWords)
{
Console.WriteLine(w);
}
}

本文代码: http://pan.baidu.com/s/1dDnJ0tV

上一篇:解决Myeclipse闪退问题


下一篇:CocoStudio基础教程(3)在程序中处理cocoStudio导出动画