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. Cocos2d-x 3.4环境安装

    电脑系统window7 32位 1.首先从官网下载cocos2d-x并解压 http://cn.cocos2d-x.org/download/ 解压后的文件夹中有一个setup.py,双击运行.需要安 ...

  2. Python开发问题和解决方案汇集

    1.Sublime Text中用Tab批量替换空格Whitespace缩进:Ctrl+A全选代码,Ctrl+Shift+P打开下拉框,输入indent,找到Convert indentation to ...

  3. 使用beanUtils操纵javabean

    Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单.易用的API操作Bean的属性——BeanUtils,在Beanutil中可以直接进行类型的自动转换. ...

  4. hover事件优化(延时操作)

    JQ的hover事件拓展 编写原因:当鼠标滑过某个带有hover事件的元素,但是仅仅是路过,并不是希望查看此部分内容的时候,效果不理想 $.fn.extend({ delayed : function ...

  5. web.xml配置error-page

    一. 通过错误码来配置error-page <error-page> <error-code>404</error-code> <location>/e ...

  6. ajax for in eval()知识点的应用

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. DHCP Set Hostname

    参考: FreeBSD DHCP Set Hostname ( Empty Hostname After Reboot ) -- http://www.cyberciti.biz/faq/freebs ...

  8. entity.Student@150f3932, entity.Student@1a740c6b 没有实体中的数据

    public class AppServerAction extends BaseAction {    /**     *      */ /**      * 初始化 “我的产品”列表 JSP页面 ...

  9. swprintf %s %ws %S 的区别

    http://www.codeproject.com/Articles/20869/D-Fast-Wavelet-Transform-Library-for-Image-Proces该作者提供的源代码 ...

  10. MyEclipse中无法识别 sun.misc.BASE64Encoder

    sun.misc.BASE64Encoder/BASE64Decoder类不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用.但是在MyEclipse中直接使用却找不到该类. 解决方法: 1 ...