參考答案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSD03
{
class Program
{
static void Main(string[] args)
{
var englishScores = new[]
{
new { name = "Sammel", score = 98 },
new { name = "Mary", score = 62 },
new { name = "Jason", score = -1 },
new { name = "Alex", score = 76 },
new { name = "John", score = 81 },
new { name = "Sherry", score = 34 },
new { name = "Adam", score = 60 }
};
var mathScores = new[]
{
new { name = "Sammel", score = 77 },
new { name = "Mary", score = 35 },
new { name = "Jason", score = 84 },
new { name = "Alex", score = 90 },
new { name = "John", score = 54 },
new { name = "Sherry", score = -1 },
new { name = "Adam", score = -1 }
};
//TODO
var query = from e in englishScores
join s in mathScores
on e.name equals s.name
where (e.score >= 60 && s.score >= 60)
select new
{
name = e.name,
eng = e.score,
math = s.score,
avg = (e.score + s.score) / 2d
};
foreach (var row in query)
{
Console.WriteLine("名字:{0},數學分數:{1},英文分數:{2},平均{3:.00}",row.name,row.math,row.eng,row.avg);
}
Console.ReadLine();
}
}
}
執行結果:
留言列表