简体   繁体   English

GroupBy 和 concat 不能一起用吗?

[英]Can GroupBy and concat not be used together?

I am trying to do some sorting based on the count() within a bundle, but I am not having much luck.我正在尝试根据捆绑中的 count() 进行一些排序,但我运气不佳。

I have this sorting that works fine by sorting all the null values last我通过最后对所有 null 值进行排序来进行这种排序

customerBasket.ItemsInBasket = customerBasket.ItemsInBasket.Where(s => s.BundleId != null)
                                                                       .OrderBy(s => s.BundleId)
                                                                       .ThenBy(s => s.ThisItemsCountWithinBundle)
                                                                       .Concat(customerBasket.ItemsInBasket.Where(s => s.BundleId == null))
                                                                       .ToList();

But then when I want to use concat again to sort "non full" bundles and put them last this cannot be done, I am trying this但是当我想再次使用 concat 对“非完整”包进行排序并将它们放在最后时,这是无法完成的,我正在尝试这个

 customerBasket.ItemsInBasket = customerBasket.ItemsInBasket.GroupBy(s => s.BundleId)
                                                                       .Where(s => s.Count() == 3)
                                                                       .Concat(customerBasket.ItemsInBasket.GroupBy(s => s.BundleId)
                                                                       .Where(s => s.Count() < 3)
                                                                       .SelectMany(s => s.ToList()));

But this gives me the following error message但这给了我以下错误消息

Severity Code Description Project File Line Suppression State Error CS1929 'IEnumerable<IGrouping<string, ItemInBasket>>' does not contain a definition for 'Concat' and the best extension method overload 'Queryable.Concat(IQueryable, IEnumerable)' requires a receiver of type 'IQueryable' slapi C:\Users\Matt\source\repos\SLAPI\Utils\BasketHelpers.cs 20 Active严重性代码 描述 项目文件行抑制 State 错误 CS1929 'IEnumerable<IGrouping<string, ItemInBasket>>' 不包含'Concat' 的定义和最佳扩展方法重载'Queryable.Concat(IQueryable, IEnumerable)' 需要接收器键入“IQueryable” slapi C:\Users\Matt\source\repos\SLAPI\Utils\BasketHelpers.cs 20 活动

Not sure how I can fix this problem.不知道如何解决这个问题。

It looks like you have a misplaced parentheses causing you problems.看起来你有一个放错位置的括号导致你的问题。 Let's split the problematic one up to an equivalent version with a few temporary variables:让我们将有问题的版本拆分为具有一些临时变量的等效版本:

var full = customerBasket.ItemsInBasket.GroupBy(s => s.BundleId)
                                       .Where(s => s.Count() == 3);
var nonFull = customerBasket.ItemsInBasket.GroupBy(s => s.BundleId)
                                          .Where(s => s.Count() < 3)
                                          .SelectMany(s => s.ToList());
customerBasket.ItemsInBasket = full.Concat(nonFull);

This is what you typed, and is giving the error.这是您输入的内容,并给出了错误。 Hopefully, this shows where the problem is coming from (SelectMany is changing the type of nonFull, so it is not something you can Concat with full).希望这能说明问题出在哪里(SelectMany 正在更改 nonFull 的类型,因此您不能将其与 full 连接)。 You need to move the SelectMany from nonFull to after the Concat.您需要将 SelectMany 从 nonFull 移到 Concat 之后。

In the version without the temporaries, this should be as simple as moving a closing parentheses.在没有临时变量的版本中,这应该像移动右括号一样简单。

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

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