【问题标题】:Create & Bind Structured Database To ListView C#创建结构化数据库并将其绑定到 ListView C#
【发布时间】:2019-08-13 08:24:42
【问题描述】:

我想通过比较他们的出生日期来删除 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

【问题讨论】:

    标签: c#


    【解决方案1】:

    尝试以下:

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

    【讨论】:

    • 感谢您的回复,Jdweng。当我将结果变量导入 ListView1 时,它会产生 Nil 值。 listView1.Items.Clear(); int counterOfArraylist = results.Count;字符串 [] str = 新字符串 [counterOfArraylist]; for (int i = 0; i
    • 在将多个项目放入列表时,您必须使用 AddRange 而不是 Add。您不能执行以下操作: listView1.Items.Add(new ListViewItem(str));请参阅:stackoverflow.com/questions/9951704/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2019-12-31
    • 2011-11-13
    • 2019-10-28
    相关资源
    最近更新 更多