在说明用法之后,先要弄点数据。

class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Region { get; set; }
public decimal Price { get; set; }
public bool IsFavorite { get; set; }
}
List<Product> products = new List<Product> {
new Product { ID=, Name="路易十八比萨饼", Region="意大利", Price=, IsFavorite = false },
new Product { ID=, Name="澳洲胡桃", Region="澳洲", Price=, IsFavorite = false },
new Product { ID=, Name="Almas鱼子酱", Region="伊朗", Price=, IsFavorite = false },
new Product { ID=, Name="和牛肉", Region="日本", Price=, IsFavorite = true },
new Product { ID=, Name="麝香猫咖啡豆", Region="印尼", Price=, IsFavorite = true },
new Product { ID=, Name="大红袍茶叶", Region="中国", Price=, IsFavorite = true },
new Product { ID=, Name="Kona Nigari矿泉水", Region="美国", Price=, IsFavorite = true },
new Product { ID=, Name="Diva伏特加", Region="北欧", Price=, IsFavorite = false },
new Product { ID=, Name="番红花的雄蕊", Region="地中海", Price=, IsFavorite = false },
};

一、ALL和ANY

bool allChina = products.All(p => p.Region == "中国");//所有项Region都要是中国,结果:False
bool anyChina = products.Any(p => p.Region == "中国");//某一项Region是中国,结果:True

二、聚集

int countIdGreater5 = products.Count(p => p.ID > );//ID大于5的记录数,结果:4
decimal maxPrice = products.Max(p => p.Price);//金额最高,结果:208000
int minId = products.Min(p => p.ID);//编号最小,结果:1
decimal avgPrice = products.Average(p => p.Price);//金额平均值,结果:53538
decimal sumPrice = products.Sum(p => p.Price);//金额总值 结果:481842

三、累加器

Product aggregate1 = products.Aggregate((total, next) =>//累加器,对products中每一个元素执行一次Func
{
total.Price += next.Price;
return total;
});

上面的代码可以做一下优化

decimal aggregate2 = products.Aggregate(2000M, (total, next) =>//累加器可以给初始值,这里给的值是2000
{
total += next.Price;
return total;//这里返回的类型和初始值一致
});

累加器操作的时候尽量用值类型,上面2段代码如果一起执行,aggregate2的值就会出现异常。

四、SELECT

string[] select1 = products.Select(p => p.Name).ToArray();//选择单列,可以转换成数组
var select2 = products.Select(p => new { p.ID, p.Name }).ToDictionary(d => d.ID);//选择两列,可以转换成键值对
var selectMore = products.Select(p => new { p.ID, p.Name, p.Price }).ToList();//选择多列,可以转换成对象
//键值对必须要保证键值是唯一的,在键值不唯一的情况可以使用ToLookup方法
var lookup = products.ToLookup(l => l.IsFavorite, p => new { p.ID, p.Name, p.Region, p.Price }).ToList();
lookup.ForEach(l =>
{
Console.WriteLine(l.Key ? "已收藏" : "未收藏");
l.ToList().ForEach(item => Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", item.ID, item.Name, item.Region, item.Price));
});

五、ORDER BY

var rightOrder = products.OrderBy(p => p.IsFavorite).ThenByDescending(p => p.ID).ToList();//主IsFavorite,次ID
var errorOrder = products.OrderBy(p => p.IsFavorite).OrderByDescending(p => p.ID).ToList();//主ID,次IsFavorite

六、GROUP BY

var group = products.GroupBy(p => p.IsFavorite).Select(g => new { IsFavorite = g.Key, SumPrice = g.Sum(item => item.Price), CountItem = g.Count() }).ToList();

当然在写拉姆达表达式的时候,也顺便说一个LINQ的用法

var groupLinq = (from p in products
group p by p.IsFavorite
into g
select new { IsFavorite = g.Key, SumPrice = g.Sum(item => item.Price), CountItem = g.Count() }).ToList();

七、WHERE

List<Product> distinct = products.Distinct().ToList();//去掉重复的记录
List<Product> take = products.Take().ToList();//顺序取3条记录
List<Product> takeWhile = products.TakeWhile(p => p.ID <= ).ToList();//只要不满足条件了,返回所有当前记录
List<Product> skip = products.Skip().ToList();//顺序跳过3条记录
List<Product> skipWhile = products.SkipWhile(p => p.Price < ).ToList();//只要不满足条件了,返回所有剩余记录
List<Product> contains = products.Where(p => p.Name.Contains("红")).ToList();//包含“红”的集合
Product first = products.Where(p => p.Name.StartsWith("大")).First();//“大”开头的第一条记录 如果无记录,直接报异常
Product lastDefault = products.Where(p => p.Name.EndsWith("胡")).LastOrDefault();//“胡”结尾的最后一条记录 如果无记录,返回默认值(对象返回null)不会报异常
Product single = products.Where(p => p.ID == ).SingleOrDefault();//取单条记录,有多条时会报异常
Product elementDefault = products.ElementAtOrDefault();//返回第10条记录 如果没有第10条记录,返回默认值(对象返回null)不会报异常

八、默认

products.DefaultIfEmpty(new Product { ID = , Name = "默认产品", Region = "默认地区", Price =  });//判断是否为空,是返回默认值,否返回products

单集合操作讲得差不多了,下面说一下多集合操作的,还是老套路,先弄点数据,这里我们数据用最普遍的DataTable格式

DataTable table1 = new DataTable();
table1.Columns.Add("ID");
table1.Columns.Add("Name");
table1.Columns.Add("Amount");
table1.Columns.Add("Description"); table1.Rows.Add("", "张三", "", "不知道和张三丰有什么关系?");
table1.Rows.Add("", "李四", "", "无");
table1.Rows.Add("", "王五", "", "是住你家隔壁的那位吗?"); DataTable table2 = new DataTable();
table2.Columns.Add("ID");
table2.Columns.Add("Name");
table2.Columns.Add("Amount");
table2.Columns.Add("Description");
table2.Rows.Add("", "张三", "", "不知道和张三丰有什么关系?");
table2.Rows.Add("", "老王", "", "这才是隔壁那位吧");
table2.Rows.Add("", "老刘", "", "无");

九、JOIN

//两表内联,结果有2条记录
var joinTable = table1.AsEnumerable().Join(table2.AsEnumerable(),
left => left["ID"].ToString(),
right => right["ID"].ToString(),
(left, right) => new {
LeftID = left["ID"].ToString(),
RightID = right["ID"].ToString(),
LeftName = left["Name"].ToString(),
RightName = right["Name"].ToString() }).ToList();
joinTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t.LeftID, t.RightID, t.LeftName, t.RightName));

十、GROUPJOIN

//以第一个表为基准,对第二个表进行分组
var groupJoinTable = table1.AsEnumerable().GroupJoin(table2.AsEnumerable(),
left => left["Description"].ToString(),
right => right["Description"].ToString(),
(key, g) => new {
Key = key["Description"].ToString(),
Count = g.Count(),
TotalAmount = g.Where(s => decimal.Parse(s["Amount"].ToString()) > ).Sum(s => decimal.Parse(s["Amount"].ToString()))
}).ToList();
groupJoinTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}", t.Key, t.Count, t.TotalAmount));

这里的统计不会包括第一个表的记录,第一个表只何为一个索引使用

十一、比较两个表是否相等

bool isEqual = table1.AsEnumerable().Where(t => t["ID"].ToString() == "")
.SequenceEqual(table2.AsEnumerable().Where(t => t["ID"].ToString() == ""), DataRowComparer.Default);
Console.WriteLine(isEqual);

这里只是做了单条记录的比较,为的只是返回一个TRUE,整个集合比较也是可以的

十二、连接两个表,不去重复,列取公共部分

var concatTable = table1.AsEnumerable().Concat(table2.AsEnumerable()).ToList();
concatTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t["ID"], t["Name"], t["Amount"], t["Description"]));

十三、差集、交集、并集

//两表的差集
var exceptTable = table1.AsEnumerable().Except(table2.AsEnumerable(), DataRowComparer.Default).ToList();
exceptTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t["ID"], t["Name"], t["Amount"], t["Description"])); //两表的交集
var intersectTable = table1.AsEnumerable().Intersect(table2.AsEnumerable(), DataRowComparer.Default).ToList();
intersectTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t["ID"], t["Name"], t["Amount"], t["Description"])); //两表的并集
var unionTable = table1.AsEnumerable().Union(table2.AsEnumerable(), DataRowComparer.Default).ToList();
unionTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t["ID"], t["Name"], t["Amount"], t["Description"]));

最后说一个比较好玩的方法,还是老规矩,先弄数据

List<Store> stores = new List<Store>
{
new Store
{
ID = ,
Name = "城北",
Products = new List<Product> {
new Product { ID=, Name="路易十八比萨饼", Region="意大利", Price=, IsFavorite = false },
new Product { ID=, Name="澳洲胡桃", Region="澳洲", Price=, IsFavorite = false },
new Product { ID=, Name="Almas鱼子酱", Region="伊朗", Price=, IsFavorite = false }
}
},
new Store
{
ID = ,
Name = "城南",
Products = new List<Product> {
new Product { ID=, Name="和牛肉", Region="日本", Price=, IsFavorite = true },
new Product { ID=, Name="麝香猫咖啡豆", Region="印尼", Price=, IsFavorite = true },
new Product { ID=, Name="大红袍茶叶", Region="中国", Price=, IsFavorite = true }
}
},
new Store
{
ID = ,
Name = "城东",
Products = new List<Product> {
new Product { ID=, Name="Kona Nigari矿泉水", Region="美国", Price=, IsFavorite = true },
new Product { ID=, Name="Diva伏特加", Region="北欧", Price=, IsFavorite = false },
new Product { ID=, Name="番红花的雄蕊", Region="地中海", Price=, IsFavorite = false }
}
}
};

我把上面的9个产品分到了3个仓库里面存在,当我要查找金额小于10000的所有产品时,按以前的做法就要写2个FOREACH循环,现在用到SELECTMANY就方便多了

var selectMany = stores.SelectMany(s => s.Products).Where(p => p.Price < ).ToList();
selectMany.ForEach(item => Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", item.ID, item.Name, item.Region, item.Price));

当然,也可以用LINQ的方式

var linqSelectMany = from s in stores
from p in s.Products
where p.Price <
select p;
linqSelectMany.ToList().ForEach(item => Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", item.ID, item.Name, item.Region, item.Price));

最新文章

  1. javascript 创建对象的7种模式
  2. Android网页中tel,sms,mailTo,Intent,Market协议用法总结
  3. poj3667【线段树】/【类似权值线段树写法】
  4. rpc远程调用开发
  5. The Toast in android
  6. [转] 关于 Eclipse 导出 Android项目 JavaDoc 详细过程
  7. $(window).height() 文档高度问题
  8. C++ list forward_list
  9. 初学者第二节之HelloWorld
  10. MUI框架 按钮点击响应不好的问题解决办法
  11. JMeter 参数化之利用JDBCConnectionConfiguration从数据库读取数据并关联变量
  12. Qt网络模块如何使用(表格)
  13. 拜占庭将军问题(Byzantine Generals Problem),一个关于分布式系统容错问题故事
  14. 集合框架-Collection与List集合
  15. Map.containsKey(String key)
  16. ZH奶酪:PHP解析URL及parse_url、parse_str、explode、implode函数说明
  17. 理解numpy exp函数
  18. Codeforces Round #441 (Div. 2)
  19. iOS: 适配启动图和图标
  20. [docker]bind9.11-with-mysql5.6 docker容器化实战

热门文章

  1. sqoop-1.4.4安装配置
  2. AndroidManifest.xml详细分析
  3. 第二章----python函数
  4. Log4j 使用
  5. SQL.Cookbook 读书笔记2 查询结果排序
  6. std::vector&lt;std::vector&lt;&gt; &gt;
  7. 清空javascript数组数据
  8. Highway
  9. TestClass必须是public的
  10. 巨蟒django之权限10,内容梳理&amp;&amp;权限组件应用