0

To simplify the scenario, let's say we have a list of People with FirstName and LastName properties.

Our data looks like this:

Bob    Smith
Jane   Smith
Todd   Grover
Larry  Lewis
Jill   Lewis
Frank  Lewis

The first step would be to add an Integer property that gets incremented for each item:

Bob    Smith  1
Jane   Smith  2
Todd   Grover 3
Larry  Lewis  4
Jill   Lewis  5
Frank  Lewis  6

Ideally, I'd like to reset the counter for every new group to achieve this:

Bob    Smith  1
Jane   Smith  2
Todd   Grover 1
Larry  Lewis  1
Jill   Lewis  2
Frank  Lewis  3

Maybe LINQ isn't appropriate. It just seems like LINQ should be able to do this elegantly.

1
  • this is the first time ever I know something to do in plain SQL but not in C# (read as "not as quickly using Linq")
    – nawfal
    Nov 25, 2012 at 21:20

3 Answers 3

8

If you just want a counter that increments with each item:

var query = people
    .Select((x, i) => new { x.FirstName, x.LastName, Index = i + 1 });

Or if you want to reset the counter for each group, grouped by LastName:

var query = people
    .GroupBy(x => x.LastName)
    .Select
    (
        x => x.Select((y, i) => new { y.FirstName, y.LastName, Index = i + 1 })
    )
    .SelectMany(x => x);

And to quickly display the query results:

foreach (var item in query)
{
    Console.WriteLine(item.FirstName + "\t" + item.LastName + "\t" + item.Index);
}
0
2

You can use the second overload of the Select method, which incorporates an index parameter passed to the lambda expression, for example:

var people = new [] {
  new { FirstName = "Bob",   LastName = "Smith"},
  new { FirstName = "Jane",  LastName = "Smith"},
  new { FirstName = "Todd",  LastName = "Grover"},
  new { FirstName = "Larry", LastName = "Lewis"},
  new { FirstName = "Jill",  LastName = "Lewis"},
  new { FirstName = "Frank", LastName = "Lewis"},
}.ToList();

people.Select((p, index) => new {
                                  FirstName = p.FirstName,
                                  LastName = p.LastName,
                                  Index = index
                                }
             );

Result:

select-index

1

Assuming there is an integer property already on the record type, and the collection is already sorted, then you can abuse Aggregate (i.e. left-fold), something like this

collection.Aggregate( (old, next) => { if (namesDiffer(old, next)) next.Index = 1 else next.Index = old.Index +1; return next;}, aRecordWithEmptyName);

EDIT -- fixed return value; fingers had been on autopilot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.