将学习LINQ后写的代码贴出来,以备今后使用,同时不断修改程序,将.NET新技术应用到这个小程序
要求:统计每门课程用户总的学习时间并加以排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace LinqApp
{
class Program
{
private static MatterDataContext db = new MatterDataContext();
public class TimeData
{
public int MatterID { get; set; }
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public double TotalSeconds { get; set; }
}
private static void GetData()
{
double totaltime = 0;
var totalData = new List<TimeData>();
var tb = db.GetTable<tMatter>();
foreach (var obj in tb)
{
var subquery = from p in db.tFreeAccountRecords
join c in db.tMatters on p.matterId equals c.Id
join f in db.tMatterResourceSets on c.resourceSetId equals f.Id
where p.matterId ==obj.Id
select new { p.matterId, f.freetime, p.hours, p.minutes, p.seconds };
foreach (var oob in subquery)
{
totaltime += new TimeSpan(oob.freetime, 0, 0).Subtract(new TimeSpan(oob.hours, oob.minutes, oob.seconds)).TotalSeconds;
}
var tcp = new TimeSpan(0,0,(int)totaltime);
totalData.Add(new TimeData { MatterID = obj.Id, Hours = (int)tcp.TotalHours, Minutes = tcp.Minutes, Seconds = tcp.Seconds, TotalSeconds = totaltime });
}
var orderbytb = from t in totalData
join t1 in db.tMatters on t.MatterID equals t1.Id
orderby t.TotalSeconds descending
select new { t, t1.name };
foreach (var c in orderbytb)
{
Console.WriteLine("---------------------------------------------------------------");
Console.WriteLine("{0} [{1}-{2}-{3} TotalS:{4}]", c.name, c.t.Hours, c.t.Minutes, c.t.Seconds, c.t.TotalSeconds);
}
Console.WriteLine("---------------------------------------------------------------");
Console.WriteLine("执行完毕");
}
static void Main(string[] args)
{
GetData();
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace LinqApp
{
class Program
{
private static MatterDataContext db = new MatterDataContext();
public class TimeData
{
public int MatterID { get; set; }
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public double TotalSeconds { get; set; }
}
private static void GetData()
{
double totaltime = 0;
var totalData = new List<TimeData>();
var tb = db.GetTable<tMatter>();
foreach (var obj in tb)
{
var subquery = from p in db.tFreeAccountRecords
join c in db.tMatters on p.matterId equals c.Id
join f in db.tMatterResourceSets on c.resourceSetId equals f.Id
where p.matterId ==obj.Id
select new { p.matterId, f.freetime, p.hours, p.minutes, p.seconds };
foreach (var oob in subquery)
{
totaltime += new TimeSpan(oob.freetime, 0, 0).Subtract(new TimeSpan(oob.hours, oob.minutes, oob.seconds)).TotalSeconds;
}
var tcp = new TimeSpan(0,0,(int)totaltime);
totalData.Add(new TimeData { MatterID = obj.Id, Hours = (int)tcp.TotalHours, Minutes = tcp.Minutes, Seconds = tcp.Seconds, TotalSeconds = totaltime });
}
var orderbytb = from t in totalData
join t1 in db.tMatters on t.MatterID equals t1.Id
orderby t.TotalSeconds descending
select new { t, t1.name };
foreach (var c in orderbytb)
{
Console.WriteLine("---------------------------------------------------------------");
Console.WriteLine("{0} [{1}-{2}-{3} TotalS:{4}]", c.name, c.t.Hours, c.t.Minutes, c.t.Seconds, c.t.TotalSeconds);
}
Console.WriteLine("---------------------------------------------------------------");
Console.WriteLine("执行完毕");
}
static void Main(string[] args)
{
GetData();
Console.ReadLine();
}
}
}