求大神帮忙指点下
class uu
{
public string Name;
public DateTime Date;
}
List<uu> list = new List<uu>();
list.Add(new uu() { Name = "张三", Date = Convert.ToDateTime("2012-3-9") });
list.Add(new uu() { Name = "李四", Date = Convert.ToDateTime("2012-3-17") });
list.Add(new uu() { Name = "王五", Date = Convert.ToDateTime("2012-4-16") });
list.Add(new uu() { Name = "李四", Date = Convert.ToDateTime("2012-3-2") });
list.Add(new uu() { Name = "张三", Date = Convert.ToDateTime("2012-3-1") });
list.Add(new uu() { Name = "张三", Date = Convert.ToDateTime("2012-5-3") });
list.Add(new uu() { Name = "王五", Date = Convert.ToDateTime("2012-6-29") });
list.Add(new uu() { Name = "赵六", Date = Convert.ToDateTime("2012-8-9") });
问题:
1、提取出有两条数据的用户
2、在提取的这些用户里面, 找出第二条数据中日期最小的
------解决方案--------------------遍历一次不就都出来了么,遍历的同时最小的日期都出来了
------解决方案-------------------- var source = list.GroupBy(t => t.Name).Where(t => t.Count() == 2).ToList();
foreach (var s in source)
{
var temp = s.OrderBy(t => t.Date).First();
Console.WriteLine(temp.Name + " " + temp.Date);
}
------解决方案--------------------
void Main()
{
List<uu> list = new List<uu>();
list.Add(new uu() { Name = "张三", Date = Convert.ToDateTime("2012-3-9") });
list.Add(new uu() { Name = "李四", Date = Convert.ToDateTime("2012-3-17") });
list.Add(new uu() { Name = "王五", Date = Convert.ToDateTime("2012-4-16") });
list.Add(new uu() { Name = "李四", Date = Convert.ToDateTime("2012-3-2") });
list.Add(new uu() { Name = "张三", Date = Convert.ToDateTime("2012-3-1") });
list.Add(new uu() { Name = "张三", Date = Convert.ToDateTime("2012-5-3") });