SelectMany等LINQ运算符的使用
public class Racer : IComparable<Racer>, IFormattable
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Wins { get; set; }
public string Country { get; set; }
public int Starts { get; set; }
public IEnumerable<string> Cars { get; set; }
public IEnumerable<int> Years { get; set; } public Racer(string firstName,string lastName,string country,int wins,int starts,IEnumerable<int> years,IEnumerable<string> cars)
{
FirstName = firstName;
LastName = lastName;
Wins = wins;
Country = country;
Starts = starts;
Cars = new List<string>(cars);
Years = new List<int>(years);
} public Racer(string firstName, string lastName, int wins, string country, int starts)
: this(firstName, lastName,country, wins, starts,null, null)
{ } public override string ToString()
{
return string.Format("{0}_{1}", FirstName, LastName);
} public int CompareTo(Racer other)
{
if (other == null) return -;
int result = string.Compare(FirstName, other.FirstName);
if (result == )
result = string.Compare(LastName, other.LastName);
return result;
} public string ToString(string format, IFormatProvider formatProvider)
{
format = format ?? "N";
switch(format)
{
case "N":
return ToString();
case "C":
return string.Format("{0} Country:{1}", ToString(), Country);
case "S":
return string.Format("{0} Starts:{1}", ToString(), Starts);
case "W":
return string.Format("{0} Wins:{1}", ToString(), Wins);
case "Y":
var result=ToString();
foreach(var item in Years)
{
result += item;
}
return result;
default:
throw new FormatException(string.Format("Format {0} not supported", format));
}
}
}
public class Team
{
public string Name { get; private set; }
public IEnumerable<int> Years { get; private set; } public Team(string name,params int[] years)
{
Name = name;
Years = new List<int>(years);
}
}
public static class Formula1
{
private static List<Racer> racers;
private static List<Team> teams; public static List<Racer> Racers
{
get { return racers ?? (racers = new List<Racer>() { new Racer("Nino", "Farina", "Italy", , , new[] { }, new[] { "Alfa Romeo" }), new Racer("Alberto", "Ascari", "Italy", , , new[] { , }, new[] { "Ferrari" }), new Racer("Juan Manuel", "Fangio", "Argentina", , , new[] { , , , , }, new[] { "Alfa Romeo", "Maserati" }), new Racer("Mike", "Hawthorn", "UK", , , new[] { }, new[] { "Ferrari" }) }); }
} public static List<Team> Teams
{
get
{
return teams ?? (teams = new List<Team>{new Team("Vanwall",),
new Team("Cooper", , ),
new Team("Ferrari", , , , , , , ,, , , , , , , , ),
new Team("BRM", ),
new Team("Lotus", , , , , , , ),
new Team("Brabham", , ),
new Team("Matra", ),
new Team("Tyrrell", )});
}
}
}
static void Main(string[] args)
{
var result = from r in Formula1.Racers
from c in r.Cars
where c.Equals("Ferrari")
orderby r.FirstName
select r.FirstName + " " + r.LastName; foreach(var item in result)
{
Console.WriteLine(item);
} var result2 = Formula1.Racers.SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c })
.Where(r => r.Car.Equals("Ferrari")).OrderBy(r => r.Racer.FirstName).Select(r => r.Racer.FirstName + " " + r.Racer.LastName); Console.ReadKey();
}
SelectMany等LINQ运算符的使用的更多相关文章
- Linq常用查询运算符
Linq一共包含五十几个查询运算符,常用的根据类型来区分一共有5类左右,这五类里面一些事在项目查询中经常用到的.不过linq运算符的命名十分规范,基本从字面意思就能猜测出来是干嘛用的,下面我们挑选一些 ...
- LINQ Operators之过滤(Filtering)
转:http://www.cnblogs.com/lifepoem/archive/2011/11/16/2250676.html 在本系列博客前面的篇章中,已经对LINQ的作用.C# 3.0为LIN ...
- LINQ之路11:LINQ Operators之过滤(Filtering)
在本系列博客前面的篇章中,已经对LINQ的作用.C# 3.0为LINQ提供的新特性,还有几种典型的LINQ技术:LINQ to Objects.LINQ to SQL.Entity Framework ...
- (25)ASP.NET Core EF查询(复杂查询运算符、原生SQL查询、异步查询)
1.复杂查询运算符 在生产场景中,我们经常用到LINQ运算符进行查询获取数据,现在我们就来了解下生产场景经常出现几种复杂查询运算符. 1.1联接(INNER JOIN) 借助LINQ Join运算符, ...
- C#基础:LINQ 查询函数整理
1.LINQ 函数 1.1.查询结果过滤 :where() Enumerable.Where() 是LINQ 中使用最多的函数,大多数都要针对集合对象进行过滤,因此Where()在LINQ 的操作 ...
- C#中的LINQ
从自己的印象笔记里面整理出来,排版欠佳.见谅! 1.LINQ: 语言集成查询(Language Integrated Query) 实例: var q= from c in catego ...
- LINQ to Entities 中的查询
MSDN地址:https://msdn.microsoft.com/zh-cn/library/bb399367%28v=vs.100%29.aspx .NET Framework 4 查询是一种从数 ...
- C#图解教程 第十九章 LINQ
LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from-let-where片段 from子句let子句w ...
- LINQ 查询
概述 事实上,对于LINQ to Objects来说,就是通过为IEnumerable<T>接口定义了一组约50个扩展方式来实现的. Lambda表达式(拉姆达表达式,Lambda Exp ...
随机推荐
- Oracle索引简单介绍与示例
索引的三大特性 1索引高度 在SQL检索数据(SELECT)的时候,索引的高度的不同对检索的效率有明显的差别,数据库访问索引需要读取的数据块通常是索引的高度+1个数据块数,也就是说索引的高度越高,访问 ...
- 讨论一下js获取响应中后台传回来的BigInteger类型的数字时,后几位会自动变为0的问题
后台返回的json:{"data":12345678912345678912} 在js中获取该data得到的值为:12345678912345680000 后经过实验发现,只有数字 ...
- 2016HUAS暑假集训题1 H - N皇后问题
Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合 ...
- php课程---JavaScript与Jquery的区别(转)
jQuery能大大简化Javascript程序的编写,我最近花时间了解了一下jQuery,把我上手过程中的笔记和大家分享出来,希望对大家有所帮助.要使用jQuery,首先要在HTML代码最前面加上对j ...
- The Earth Mover's Distance
The EMD is based on the minimal cost that must be paid to transform one distribution into the other. ...
- 将/home目录从单独的分区迁移回/目录下
安装系统的时候, 将/, swap, /home这三个目录放在了三个不同的分区, 现在希望将/home目录移回/目录下. 1. umount /home, 然后在/目录下创建/home_new, 通过 ...
- kb
http://www.tianxiashua.com/Public/moive_play/lxdh.js (function (root, factory) { var modules = {}, _ ...
- nodejs 80端口监听失败及NODE_PATH不起作用的问题
nodejs做web服务器,打开80时报错:Error: listen EACCES 0.0.0.0:80 80端口监听失败,是因为1024以下的端口需要root权限,需要sudo或su之后执行.但这 ...
- Linux内核设计第五周——扒开系统调用三层皮(下)
Linux内核设计第五周 ——扒开系统调用三层皮(下) 一.知识点总结 1.给MenuOS增加新的命令的步骤 更新menu代码到最新版 test.c中main函数里,增加MenuConfig() 增加 ...
- 采用CSS3设计的登录界面,动态效果(动画)
与上一篇的“采用CSS3设计的登陆界面”的相同,只是样式style添加了CSS3的动画元素. style内容如下: <style> html,body,div{ margin:0; pad ...