Samantha J T Star
Samantha J T Star

Reputation: 32758

I need to add a unique id to instances of a collection using LINQ

I have the following class that is used to hold data for reporting only.

public class AdminDetail
{
    public int    Row { get; set; }   // This row needs a unique number 
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Title { get; set; }
    public string Status { get; set; }
    public string Type { get; set; }
    public string Level { get; set; }
    public string Order { get; set; }
}

Populated with the following select on the _table collection.

 details = from t in _table
                      select new AdminDetail
                      {
                          PartitionKey = t.PartitionKey,
                          RowKey = t.RowKey,
                          Title = t.Title,
                          Status = t.Status,
                          Type = t.Type,
                          Level = t.Level,
                          Order = t.Order
                      };

            detailsList = details.OrderBy(item => item.Order).ThenBy(item => item.Title).ToList();

After the rows are sorted with the last statement I would like to give to put a value into the RowID that corresponds to the row. So if I had three instances returned I would like them to have the RowID 1,2 and 3.

Is there a way that I can do this with LINQ?

Upvotes: 0

Views: 117

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1500055

You could do it using the Select overload which provides an index, but you'd probably want to do the projection to AdminDetail after the ordering:

var ordered = from item in _table
              orderby item.Order, item.Title
              select item;

var details = ordered.Select((t, index) => new AdminDetail
                             {
                                 PartitionKey = t.PartitionKey,
                                 RowKey = t.RowKey,
                                 Title = t.Title,
                                 Status = t.Status,
                                 Type = t.Type,
                                 Level = t.Level,
                                 Order = t.Order,
                                 Row = index + 1
                             })
                      .ToList();

Aside from anything else, this way if _table is a LINQ to SQL table (or something similar) the ordering can be performed in the database rather than in LINQ to Objects.

Upvotes: 2

dknaack
dknaack

Reputation: 60448

Description

You can use the select method with an index.

Sample

detailsList = details.OrderBy(item => item.Order).ThenBy(item => item.Title)
    .Select((t, index) => new AdminDetail()
    {
        PartitionKey = t.PartitionKey,
        RowKey = t.RowKey,
        Title = t.Title,
        Status = t.Status,
        Type = t.Type,
        Level = t.Level,
        Order = t.Order,
        Row = index + 1
    }).ToList();

More Information

Upvotes: 2

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Use Select method with index, e.g.:

table.Select((t, index) => new AdminDetail { 
   Row = index + 1, 
   PartitionKey = t.PartitionKey,
   RowKey = t.RowKey,
   Title = t.Title,
   Status = t.Status,
   Type = t.Type,
   Level = t.Level,
   Order = t.Order });

Upvotes: 2

Related Questions