如何使用 LINQ 在数据网格视图中不打印重复项

发布于 2024-10-27 04:34:46 字数 67 浏览 3 评论 0原文

我有一个名为 TIME 的表,里面有多个学生。有时,学生 A 在 TIME 中会有多个条目。如何只打印学生A的最新记录?

I have a table called TIME and there are multiple students in it. Sometimes, student A will have more than one entry in TIME. How do I print just the latest record for student A?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疑心病 2024-11-03 04:34:46

你想要最新的学生A?..那么你可能需要这样的东西:

dataGridView.DataSource = students.GroupBy(s => s.Name).
Select(i => i.OrderByDescending(s => s.DateChanged).First());

我假设你可以通过一些参数对它们进行排序(我在这里使用了DateChanged..可能你有一些增量主键或smth)。

You want latest student A?.. Then may be you need something like this:

dataGridView.DataSource = students.GroupBy(s => s.Name).
Select(i => i.OrderByDescending(s => s.DateChanged).First());

I assume here that you can sort them by some parameter(i used DateChanged here..May be you have some incremental primary key or smth).

始终不够爱げ你 2024-11-03 04:34:46

此示例使用 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

This sample uses Distinct to remove duplicate elements in a sequence of factors of 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);
    }
}

Result

Prime factors of 300:
2
3
5
See: http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#distinct1

黑色毁心梦 2024-11-03 04:34:46
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; }
}
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; }
}
太阳哥哥 2024-11-03 04:34:46

您必须按学生入学时间对记录进行分组。

from r in StudentRecords
group r by r.StudentId into g
select new { StudentId = g.Key, MostRecentEntry = g.Max(e => e.EntryTime)}

因为我不知道你的模式,所以我使用了一些任意名称

You have to group the records by student entry time.

from r in StudentRecords
group r by r.StudentId into g
select new { StudentId = g.Key, MostRecentEntry = g.Max(e => e.EntryTime)}

since I don't know your schema, I've used some arbitrary names

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文