简体   繁体   English

如何编写此linq查询?

[英]How do I write this linq query?

I have this code that totally works fine. 我有完全可以正常工作的这段代码。 But I would like to understand how to write it as a single linq query rather than a ling query, and a for loop containing another repeating query. 但是我想了解如何将其编写为单个linq查询而不是ling查询,以及包含另一个重复查询的for循环。

The idea is that I am looking at database of classes for which people have not completed their work. 我的想法是,我正在查看尚未完成工作的班级数据库。 In the first query, I am finding all the people in the database uniquely. 在第一个查询中,我在数据库中找到了所有唯一的人。 Then in the second query in the for loop, I am iterating though all the names from the first query and counting how many classes they still have left to complete. 然后,在for循环的第二个查询中,我遍历第一个查询中的所有名称,并计算它们还有多少类需要完成。

Again, the code works, I just wondering the best way to consolidate it into one single query. 再次,代码有效,我只是想知道将其整合为一个查询的最佳方法。

        var ListOfNames = (from record in records select record.LastName).Distinct();
        foreach (var NameOfPerson in ListOfNames)
        {
            Console.WriteLine(NameOfPerson);
            var NotCompletedCount = (from rec in records
                                     where rec.LastName == NameOfPerson 
                                     && rec.AssignmentType == "MANDATORY" 
                                     && string.IsNullOrEmpty(rec.CompleteDate) == true
                                     select rec).Count();
            Console.WriteLine(NotCompletedCount);
            rankList.Add(new Rank{ LastName = NameOfPerson, RankCount = NotCompletedCount});
        }

I'm not sure that there are differences between previous answer ant this. 我不确定以前的答案是否与此有所不同。 In this case we firstly filter data and then Group by name and Select Rank instances. 在这种情况下,我们首先过滤数据,然后按名称分组和选择等级实例。 I think in the result .Net will optimize and make the same query to the DB. 我认为,.Net结果将优化并向数据库进行相同的查询。

var result = records.Where(x => x.AssigmentType == "MANDATORY" && 
string.IsNullOrEmpty(x.CompleteDate))
    .GroupBy(x => x.LastName)
    .Select(group => new Rank {LastName = group.Key, RankCount = group.Count()});

More details how GroupBy works is here: LINQ with groupby and count GroupBy的工作原理的更多详细信息在这里: LINQ与groupby和count

As I understand what you are doing is basically for each last name, get the count of records that meets some condition. 据我了解,您的工作基本上是针对每个姓,获取满足某种条件的记录数。 GroupBy can be used here: 可在此处使用GroupBy

var rankList = records.GroupBy(r => r.LastName)
                      .Select(g => new Rank { 
                                        LastName = g.Key, 
                                        RankCount = g.Count(x => x.AssignmentType == "MANDATORY" && string.IsNullOrEmpty(x.CompleteDate))).ToList()

Sorry the formatting sucks because the Count call is bit long. 抱歉,由于Count调用太长,格式化很烂。

This combines your foreach loop and the first query together. 这将您的foreach循环和第一个查询结合在一起。 It groups records by LastName , creates a Rank instance for each name calculating the count of records that meets your criteria for that name. 它按LastName记录进行分组,为每个名称创建一个Rank实例,以计算满足您对该名称标准的记录数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM