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运算符的使用的更多相关文章

  1. Linq常用查询运算符

    Linq一共包含五十几个查询运算符,常用的根据类型来区分一共有5类左右,这五类里面一些事在项目查询中经常用到的.不过linq运算符的命名十分规范,基本从字面意思就能猜测出来是干嘛用的,下面我们挑选一些 ...

  2. LINQ Operators之过滤(Filtering)

    转:http://www.cnblogs.com/lifepoem/archive/2011/11/16/2250676.html 在本系列博客前面的篇章中,已经对LINQ的作用.C# 3.0为LIN ...

  3. LINQ之路11:LINQ Operators之过滤(Filtering)

    在本系列博客前面的篇章中,已经对LINQ的作用.C# 3.0为LINQ提供的新特性,还有几种典型的LINQ技术:LINQ to Objects.LINQ to SQL.Entity Framework ...

  4. (25)ASP.NET Core EF查询(复杂查询运算符、原生SQL查询、异步查询)

    1.复杂查询运算符 在生产场景中,我们经常用到LINQ运算符进行查询获取数据,现在我们就来了解下生产场景经常出现几种复杂查询运算符. 1.1联接(INNER JOIN) 借助LINQ Join运算符, ...

  5. C#基础:LINQ 查询函数整理

    1.LINQ 函数   1.1.查询结果过滤 :where() Enumerable.Where() 是LINQ 中使用最多的函数,大多数都要针对集合对象进行过滤,因此Where()在LINQ 的操作 ...

  6. C#中的LINQ

    从自己的印象笔记里面整理出来,排版欠佳.见谅!   1.LINQ: 语言集成查询(Language Integrated Query) 实例: var q=      from c in catego ...

  7. LINQ to Entities 中的查询

    MSDN地址:https://msdn.microsoft.com/zh-cn/library/bb399367%28v=vs.100%29.aspx .NET Framework 4 查询是一种从数 ...

  8. C#图解教程 第十九章 LINQ

    LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from-let-where片段 from子句let子句w ...

  9. LINQ 查询

    概述 事实上,对于LINQ to Objects来说,就是通过为IEnumerable<T>接口定义了一组约50个扩展方式来实现的. Lambda表达式(拉姆达表达式,Lambda Exp ...

随机推荐

  1. 您的服务器没有安装这个PHP扩展:OpenSSL(其他平台API同步需要)

    今天在安装一个博客系统的时候提示这个错,在网上找了半天,自己慢慢弄出来的,具体如下: 1.找到你的php.ini 文件,将“;extension=php_openssl.dll”前面分号去掉. 2.复 ...

  2. div自定义的滚动条 (水平导航条)

    <!DOCTYPE html> <html> <head> <title></title> <style> div{ /* wi ...

  3. js打开没有地址栏下拉条新窗口

    <script type="text/javascript" language="javascript"> function vNodeAuditL ...

  4. Bootstrap 固定定位(Affix)

    来自:慕课网 http://www.imooc.com/code/5396 Affix 效果常见的有以下三种: ☑ 顶部固定 ☑ 侧边栏固定 ☑ 底部固定 固定定位--声明式触发固定定位 Affix ...

  5. python的内存管理机制

    先从较浅的层面来说,Python的内存管理机制可以从三个方面来讲 (1)垃圾回收 (2)引用计数 (3)内存池机制 一.垃圾回收: python不像C++,Java等语言一样,他们可以不用事先声明变量 ...

  6. DOM概述

    <!-- DOM:Document Object Model 文档对象模型 用来将标记型文档封装成对象,并将标记型文档中的所有内容(标签,文本,属性等)都封装成对象 封装成对象的目的是为了更为方 ...

  7. 手机端开发icon的问题

    一般来说,手机端的图片能用字体(字体小的情况下)的话,效果更好,因为不受图片缩放的失真影响. 但是有时,用位图的话,图片材料要高清晰,用jpg的高质量. 另外,有彩图与灰度图的情况下,考虑使用css3 ...

  8. centos同步北京时间

    yum install ntp ntpdate #ntpdate -u 202.120.2.101 //写入硬件 #hwclock -w 以下是国内常见的NTP服务器 ntp.sjtu.edu.cn ...

  9. Wordpress基础:文章和页面的区别

    页面: 页面是你可以单独建立一个固定页面,可以作为留言板,或者通知的单页面,发布之后是固定的网址. 页面并不能被分类.亦不能拥有标签,但是它们可以有层级关系.您可将页面附属在另一个页面之下. 对应模板 ...

  10. Python开发【第五章】:Python常用模块

    一.模块介绍: 1.模块定义 用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质上就是.py结尾python文件 分类:内置模块.开源模块.自定义模块 2.导入模块 本质:导 ...