Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq Groupby不在c中分组_C#_Entity Framework_Linq_Vue.js_Entity - Fatal编程技术网

C# Linq Groupby不在c中分组

C# Linq Groupby不在c中分组,c#,entity-framework,linq,vue.js,entity,C#,Entity Framework,Linq,Vue.js,Entity,下面我有一段代码,它根据客户机输出约会列表,一些客户机可以有多个约会,但最新的一个是需要为所述客户机输出的约会 输出根本没有分组,出于某种原因,我无法理解为什么没有分组 foreach (ClientRecord client in clients) { List<ReturnRecord> records = db.Appointments .AsNoTracki

下面我有一段代码,它根据客户机输出约会列表,一些客户机可以有多个约会,但最新的一个是需要为所述客户机输出的约会

输出根本没有分组,出于某种原因,我无法理解为什么没有分组

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);
下面是输出屏幕抓取的附件

您需要将GroupBy的结果分配给变量:

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

确保实际使用.GroupBy方法返回的新IEnumerable

如果要返回列表,需要使用变通方法:

从.GroupBy中获取IEnumerable 使用.SelectMany可选择所有元素并将其保存到IEnumerable中 现在,您可以使用.List将IEnumerable转换为列表 例如:

//更长的选择 IEnumerable groups=resultList .GroupByrec=>rec.PropertyAddress; IEnumerable result=groups.SelectManygroup=>group; List listResult=result.ToList; 返回OklistResult; //较短的备选方案 IEnumerable groups=resultList .GroupByrec=>rec.PropertyAddress; IEnumerable result=groups.SelectManygroup=>group; 返回Okresult.ToList;
GroupBy方法有一个返回值!它不是空的。您需要捕捉返回值。它不会简单地修改调用该方法的源集合。请阅读此.GroupBy,因为基本上所有LINQ方法都不是一个操作,而是一个返回结果的函数,而您不使用该函数。如何使用不可编译的代码进行回答以获得3次投票?请编辑您的帖子,并填写正确的回复类型。使您的代码至少可编译,因为此答案部分错误且不可编译