简体   繁体   English

在 LINQ 中的 orderby 之后对每组的行进行编号

[英]Numbering the rows per group after an orderby in LINQ

Say you have columns AppleType, CreationDate and want to order each group of AppleType by CreationDate.假设您有 AppleType、CreationDate 列,并希望按 CreationDate 对每组 AppleType 进行排序。 Furthermore, you want to create a new column which explicitly ranks the order of the CreationDate per AppleType.此外,您希望创建一个新列,该列明确排列每个 AppleType 的 CreationDate 的顺序。

So, the resulting DataSet would have three columns, AppleType, CreationDate, OrderIntroduced.因此,生成的 DataSet 将包含三列,AppleType、CreationDate、OrderIntroduced。

Is there a LINQ way of doing this?有没有这样做的 LINQ 方式? Would I have to actually go through the data programmatically (but not via LINQ), create an array, convert that to a column and add to the DataSet?我是否必须以编程方式(但不是通过 LINQ)通过数据实际 go,创建一个数组,将其转换为列并添加到 DataSet? I have there is a LINQ way of doing this.我有这样做的 LINQ 方式。 Please use LINQ non-method syntax if possible.如果可能,请使用 LINQ 非方法语法。

So are the values actually appearing in the right order?那么这些值实际上是否以正确的顺序出现? If so, it's easy - but you do need to use method syntax, as the query expression syntax doesn't support the relevant overload:如果是这样,这很容易 - 但您确实需要使用方法语法,因为查询表达式语法不支持相关的重载:

var queryWithIndex = queryWithoutIndex.Select((x, index) => new
                                              {
                                                  x.AppleType,
                                                  x.CreationDate,
                                                  OrderIntroduced = index + 1,
                                              });

(That's assuming you want OrderIntroduced starting at 1.) (假设您希望OrderIntroduced从 1 开始。)

I don't know offhand how you'd then put that back into a DataSet - but do you really need it in a DataSet as opposed to in the strongly-typed sequence?我不知道你是如何将它放回DataSet的——但你真的需要它在DataSet中而不是在强类型序列中吗?

EDIT: Okay, the requirements are still unclear, but I think you want something like:编辑:好的,要求还不清楚,但我认为你想要这样的东西:

var query = dataSource.GroupBy(x => x.AppleType)
     .SelectMany(g => g.OrderBy(x => x.CreationDate)
                       .Select((x, index ) => new {
                               x.AppleType,
                               x.CreationDate,
                               OrderIntroduced = index + 1 }));

Note: The GroupBy and SelectMany calls here can be put in query expression syntax, but I believe it would make it more messy in this case.注意:这里的GroupBySelectMany调用可以放在查询表达式语法中,但我相信在这种情况下会变得更加混乱。 It's worth being comfortable with both forms.值得对 forms 感到满意。

If you want a pure Linq to Entities/SQL solution you can do something like this:如果您想要一个纯 Linq 到实体/SQL 解决方案,您可以执行以下操作:

Modified to handle duplicate CreationDate's修改以处理重复的 CreationDate

var query = from a in context.AppleGroup
            orderby a.CreationDate
            select new
            {
              AppleType = a.AppleType,
              CreationDate = a.CreationDate,
              OrderIntroduced = (from b in context.AppleGroup
                                 where b.CreationDate < a.CreationDate
                                 select b).Count() + 1
            };

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

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