using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 学生成绩Linq
{
class Student
{
public string LastName;
public List<int> Scores;
public Student()
{
}
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student> { new Student
{
LastName="xiaogui",Scores=new List<int>{97,42,91,60}}, new Student
{
LastName="xiaozhan",Scores=new List<int>{50,92,81,60}}, new Student
{
LastName="xiaolan",Scores=new List<int>{32,32,81,90}}, new Student
{
LastName="xiaowan",Scores=new List<int>{92,22,81,60}},
}; var query = from stuent in students
from score in stuent.Scores
where score > 90
select new
{
Last = stuent.LastName,
score
};
foreach (var student in query)//大于90分的显示出来
{
Console.WriteLine("{0} Score:{1}", student.Last, student.score);
}
DataTable dt = new DataTable();
dt.Columns.Add("编号", typeof(string));
dt.Columns.Add("单价", typeof(Int32));
dt.Columns.Add("数量", typeof(Int32));
dt.Rows.Add("标识1", 100,11);
dt.Rows.Add("标识2", 30,5);
dt.Rows.Add("标识2", 40,5);
dt.Rows.Add("标识3", 80,20);
var query1 = from c in dt.AsEnumerable()
group c by c.Field<string>("编号")
into g
let c = g.First().Field<Int32>("数量")
select new
{
zhujian = g.Key,
tongji = g.Sum(s => s.Field<Int32>("单价")),
danjia=c,
zj= g.Sum(s => s.Field<Int32>("单价")) * c
};
Console.WriteLine("编号" + " 数量 单价 合计" + "\n" + "=================");
foreach (var item in query1)
{
Console.WriteLine("" + item.zhujian + " " + item.tongji + " " + item.danjia + " " + item.zj);
}
Console.ReadLine();
}
}
}
xiaogui Score:97
xiaogui Score:91
xiaozhan Score:92
xiaowan Score:92
编号 数量 单价 合计
=================
标识1 100 11 1100
标识2 70 5 350
标识3 80 20 1600