C# 我需要使用LINQ向集合的实例添加唯一id
我有一个类,它只用于保存用于报告的数据C# 我需要使用LINQ向集合的实例添加唯一id,c#,.net,linq,C#,.net,Linq,我有一个类,它只用于保存用于报告的数据 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; } publ
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; }
}
在_表集合上填充以下select
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();
在使用最后一条语句对行进行排序之后,我想在RowID中输入一个值
对应于行的。因此,如果返回三个实例,我希望它们具有RowID
1、2和3
有没有一种方法可以通过LINQ实现这一点?使用带有索引的方法,例如:
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 });
使用带有索引的方法,例如:
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 });
描述
您可以使用带有索引的select
方法
样品
更多信息
select
方法
样品
更多信息
AdminDetail
:
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();
除此之外,如果\u table
是LINQ to SQL表(或类似的表),则可以在数据库中执行排序,而不是在LINQ to Objects中执行排序。您可以使用进行排序,但您可能希望在排序后投影到AdminDetail
:
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();
除此之外,如果\u table
是LINQ to SQL表(或类似的表),则可以在数据库中而不是在LINQ to对象中执行排序