我想通过比较出生日期来删除ListView1中除所有组中最小的学生以外的所有项目。
因此,我尝试创建一个结构化的数据库并将其绑定到我的ListView1,但是由于错误和异常而失败。
非常感谢您的帮助。
这是我的代码-
// assume 'Students is a List<Student>
IEnumerable<Student> earlydatestudents = Students.GroupBy(std => std.Group)
.Select(grp =>
{
DateTime dt = grp.Min(s => s.DOB);
return grp.Where(st => st.DOB == dt);
})
.SelectMany(slist => slist);
var toDeleteList = Students.Except(earlydatestudents).ToList();
//
我的ListView1包含-
Student , DOB , Location
Group1
AAA 10-05-2000 Mumbai
BBB 05-02-2000 Pune
CCC 01-01-2000 Delhi
Group2
DDD 20-03-1999 Lucknow
EEE 15-06-1999 Chennai
FFF 18-09-1999 Ahmedabad
答案 0 :(得分:0)
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace ConsoleApplication106
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>() {
new Student() { name = "AAA", dob = DateTime.ParseExact("10-05-2000", "dd-MM-yyyy", CultureInfo.InvariantCulture), location = "Mumbai"},
new Student() { name = "BBB", dob = DateTime.ParseExact("05-02-2000", "dd-MM-yyyy", CultureInfo.InvariantCulture), location = "Pune"},
new Student() { name = "CCC", dob = DateTime.ParseExact("01-01-2000", "dd-MM-yyyy", CultureInfo.InvariantCulture), location = "Delhi"},
new Student() { name = "DDD", dob = DateTime.ParseExact("20-03-1999", "dd-MM-yyyy", CultureInfo.InvariantCulture), location = "Lucknow"},
new Student() { name = "EEE", dob = DateTime.ParseExact("15-06-1999", "dd-MM-yyyy", CultureInfo.InvariantCulture), location = "Chennai"},
new Student() { name = "FFF", dob = DateTime.ParseExact("18-09-1999", "dd-MM-yyyy", CultureInfo.InvariantCulture), location = "Ahmedabad"}
};
var results = students.OrderByDescending(x => x.dob) //sort from youngest to oldest
.GroupBy(x => x.dob.Year) //group by year
.Select(x => x.First()) //get first student born each year which is youngest
.ToList();
}
}
public class Student
{
public DateTime dob { get; set; }
public string name { get; set; }
public string location { get; set;}
}
}