简体   繁体   English

Linq Groupby 不在c#分组

[英]Linq Groupby Not Grouping in c#

Below i have a snippet of code which outputs a list of Appointments based on clients, some clients can have more than one appointment but the latest one is the one that needs to be outputted for said client下面我有一段代码,它根据客户输出约会列表,一些客户可以有多个约会,但最新的是需要为所述客户输出的约会

the output is not grouping at all and for some reason i cannot figure why the heck not output 根本没有分组,出于某种原因我不知道为什么不分组

foreach (ClientRecord client in clients)
                {
                    List<ReturnRecord> records = db.Appointments
                        .AsNoTracking()
                        .Include(rec => rec.Property)
                        .Include(rec => rec.Property.Address)
                        .Include(rec => rec.AppointmentType)
                        .ToList()
                        .Where(rec => rec.ClientID == client.ID)
                        .Select(rec => new ReturnRecord
                        {
                            ClientName = $"{client.FirstNames} {client.Surnames}",
                            PropertyAddress = $"{rec.Property.Address.FormattedAddress}",
                            AppStatus = $"{rec.AppointmentStatus.Name}",
                            StockStatus = $"{rec.Property.Stocks.FirstOrDefault().StockStatus.Name}",
                            LastUpdated = rec.LastUpdated
    
                        })
                        .ToList();
    
                    returnList.AddRange(records);
    
                }
                returnList.GroupBy(rec => rec.PropertyAddress);
    
                return Ok(returnList);

here is an attachment of the screen grab of the output这是 output 屏幕截图的附件在此处输入图像描述

You need to assign result of GroupBy() to variable:您需要将 GroupBy() 的结果分配给变量:

returnList = returnList.GroupBy(rec => rec.PropertyAddress).ToList();

Make sure to actually use the new IEnumerable that the .GroupBy() Method returned.确保实际使用.GroupBy()方法返回的新 IEnumerable。

If you want to return a List you need to use a workaround:如果你想返回一个列表,你需要使用一个解决方法:

  1. Get the IEnumerable<IGrouping<int, ReturnRecord>> from the .GroupBy().GroupBy()获取IEnumerable<IGrouping<int, ReturnRecord>>
  2. Use .SelectMany() to select all elements and save them into an IEnumerable使用.SelectMany()到 select 所有元素并将它们保存到 IEnumerable
  3. Now you can convert your IEnumerable into a List with .List()现在您可以使用.List()将 IEnumerable 转换为 List

Example:例子:

// Longer Alternative
IEnumerable<IGrouping<int, ReturnRecord>> groups = resultList
    .GroupBy((rec => rec.PropertyAddress);
IEnumerable<ReturnRecord> result = groups.SelectMany(group => group);
List<ReturnRecord> listResult = result.ToList();
return Ok(listResult);

// Shorter Alternative
IEnumerable<IGrouping<int, ReturnRecord>> groups = resultList
    .GroupBy((rec => rec.PropertyAddress);
IEnumerable<ReturnRecord> result = groups.SelectMany(group => group);
return Ok(result.ToList());

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

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