SelectMany等LINQ运算符的使用

时间:2023-03-09 09:30:14
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();
}