我有一张名为TIME的桌子,里面有多个学生。有时,学生A将在TIME中有多个条目。如何打印学生A的最新记录?
答案 0 :(得分:2)
你想要最新的学生A?..那么你可能需要这样的东西:
dataGridView.DataSource = students.GroupBy(s => s.Name).
Select(i => i.OrderByDescending(s => s.DateChanged).First());
我在这里假设你可以通过一些参数对它们进行排序(我在这里使用了DateChanged ......可能你有一些增量主键或smth)。
答案 1 :(得分:0)
此示例使用Distinct删除因子序列为300的重复元素。
public void Linq46()
{
int[] factorsOf300 = { 2, 2, 3, 5, 5 };
var uniqueFactors = factorsOf300.Distinct();
Console.WriteLine("Prime factors of 300:");
foreach (var f in uniqueFactors)
{
Console.WriteLine(f);
}
}
结果
300的主要因素:
2
3
5
请参阅:http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#distinct1
答案 2 :(得分:0)
void Main ()
{
var rows = new [] {
new Student { Name = "A", Timestamp = DateTime.Now.AddHours (-1) },
new Student { Name = "A", Timestamp = DateTime.Now.AddHours (-3) },
new Student { Name = "B", Timestamp = DateTime.Now.AddHours (4) },
new Student { Name = "B", Timestamp = DateTime.Now.AddHours (1) },
new Student { Name = "B", Timestamp = DateTime.Now }
};
var recentRows = from row in rows
group row by row.Name into studentRows
select studentRows.OrderByDescending (sr => sr.Timestamp).First ();
Console.WriteLine (recentRows);
}
class Student {
public string Name { get; set; }
public DateTime Timestamp { get; set; }
}
答案 3 :(得分:0)
您必须按学生入学时间对记录进行分组。
from r in StudentRecords
group r by r.StudentId into g
select new { StudentId = g.Key, MostRecentEntry = g.Max(e => e.EntryTime)}
因为我不知道你的架构,所以我使用了一些任意名称