-1

I want to delete all items in ListView1 except youngest student in each group by comparing their date of births.

So, I tried to create a structured database and binding it to my ListView1 but failed with errors & exceptions.

Any help is greatly appreciated.

This is my code-

// 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();

// 

My ListView1 contains-

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

1 Answer 1

0

Try following :

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;}
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, Jdweng. When I imported the results variable into ListView1, it results in Nil value. listView1.Items.Clear(); int counterOfArraylist = results.Count; string[] str = new string[counterOfArraylist]; for (int i = 0; i < str.Length; i++) { str[i] = results[i].ToString(); } listView1.Items.Add(new ListViewItem(str));
You have to use AddRange instead of Add when putting more than one item into a list. You can't do following : listView1.Items.Add(new ListViewItem(str)); See : stackoverflow.com/questions/9951704/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.