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 ...
随机推荐
- Codeforces Round #196 (Div. 2)
A 题意:O(-1) 思路:排个序搞定. B 题意:O(-1) 思路:坑了我好久,这个框框水平垂直比例固定,分两种情况即可,不能旋转,我想多了,分了四种情况. C 题意:一列n个位置,让你填m个数,当 ...
- tomcat域名问题
首先可以确认,tomcat支持以下的配置: 1.同一个tomcat配置多个端口来发布不同的应用,配置多个<Service>即可; 2.同一个tomcat可以配置多个虚拟主机,以指定不同的域 ...
- window常用命令(持续更新)
1.netstat -ano 查看端口占用情况 可以根据PID到任务管理器中找到对应的进程 2.tasklist|findstr 进程号(pid) 查看pid是哪个程序启用的 3.taskkill ...
- c语言.大数的输出
转化成字符串,再用for循环输出: #include <stdio.h>#include <string.h>int main(){ char s[32]; int d, ...
- php使用strlen()判断中文汉字字符串长度
php使用strlen()判断中文汉字字符串长度 对于含有中文情况,此时可以采用: iconv_strlen($str,"UTF-8"); iconv_strlen 是无论是何种编 ...
- 给RecyclerView实现的GridView加上HeaderView和FooterView
给RecyclerView设置布局管理器 GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); 写适配器,添加子项 ...
- loadrunner获取返回值为乱码
找了很多方法,utf-8也设置了,还是不行,只有有转码方法了 web_reg_save_param("res2", "LB=\"msg\":\&quo ...
- centos安装后iptables基本设置
一.首先关闭防火墙#service iptables stop 二.查看状态,确认关闭#service iptables status 三.清除掉防火墙规则#iptables -F#iptables ...
- 如何获取imageView中当前内容的相关信息并比较?
public class MainActivity extends Activity implements OnClickListener{ private Button button; privat ...
- Kafka组件监控
Kafka web console http://blog.csdn.net/hengyunabc/article/details/40431627 KafkaOffsetMonitor http:/ ...