gpt4 book ai didi

c# - 如何将唯一的行号添加到我的 Linq 选择中?

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:18 25 4
gpt4 key购买 nike

我有以下代码:

    public IEnumerable<Content.Grid> DetailsBase(string pk)
{
var data = contentRepository.GetPk(pk);
var refType = this.GetRefType(pk);
var refStat = this.GetRefStat(pk);
var type = referenceRepository.GetPk(refType);
var stat = referenceRepository.GetPk(refStat);
var dataOut =
from d in data
join s in stat on d.Status equals s.RowKey into statuses
from s in statuses.DefaultIfEmpty()
join t in type on d.Type equals t.RowKey into types
from t in types.DefaultIfEmpty()
select new Content.Grid
{
PartitionKey = d.PartitionKey,
RowKey = d.RowKey,
Order = d.Order,
Title = d.Title,
Status = s == null ? "" : s.Value,
StatusKey = d.Status,
Type = t == null ? "" : t.Value,
TypeKey = d.Type
};
return dataOut;
}

和这个类:

    public class Grid
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
// Counter
public int Row { get; set; }
//
public int Order { get; set; }
public string Title { get; set; }
public string Status { get; set; }
public string StatusKey { get; set; }
public string Type { get; set; }
}

有没有办法可以将 Row 的值设置为递增。在另一个选择中,我使用了这个:

        return dataIn
.OrderBy(item => item.Order)
.Select((t, index) => new Content.Grid()
{
PartitionKey = t.PartitionKey,
RowKey = t.RowKey,
Row = index + 1,

有什么方法可以让我在第一次选择时使用类似的东西吗?

更新:

我有以下似乎有效但不确定如何加入这两个选择:

    public IList<Content.Grid> GetContentGrid(string pk)
{
var data =
from d in contentRepository.GetPk(pk)
join s in referenceRepository.GetPk(this.GetRefStat(pk))
on d.Status equals s.RowKey into statuses
from s in statuses.DefaultIfEmpty()
join t in referenceRepository.GetPk(this.GetRefType(pk))
on d.Type equals t.RowKey into types
from t in types.DefaultIfEmpty()
select new Content.Grid
{
PartitionKey = d.PartitionKey,
RowKey = d.RowKey,
Order = d.Order,
Title = d.Title,
Status = s == null ? "" : s.Value,
StatusKey = d.Status,
Type = t == null ? "" : t.Value,
TypeKey = d.Type,
Link = d.Link,
Notes = d.Notes,
TextLength = d.TextLength,
// AuditableTable
Created = d.Created ?? new DateTime(2012, 1, 1),
CreatedBy = d.CreatedBy ?? "n/a",
Modified = d.Modified ?? new DateTime(2012, 1, 1),
ModifiedBy = d.ModifiedBy ?? "n/a"
};
return data
.OrderBy(item => item.Order)
.Select((t, index) => new Content.Grid()
{
PartitionKey = t.PartitionKey,
RowKey = t.RowKey,
Row = index + 1,
Order = t.Order,
Title = t.Title,
Status = t.Status,
StatusKey = t.StatusKey,
Type = t.Type,
TypeKey = t.TypeKey,
Link = t.Link,
Notes = t.Notes,
TextLength = t.TextLength,
})
.ToList();
}

最佳答案

您不能在查询表达式中指定与索引相关的投影,但您可以这样做:

    var dataOut = from d in data
join s in stat on d.Status equals s.RowKey into statuses
from s in statuses.DefaultIfEmpty()
join t in type on d.Type equals t.RowKey into types
from t in types.DefaultIfEmpty()
select new { d, s, t };
return dataOut.Select((x, index) => new Content.Grid {
PartitionKey = x.d.PartitionKey,
RowKey = x.d.RowKey,
Order = x.d.Order,
Title = x.d.Title,
Status = x.s == null ? "" : x.s.Value,
StatusKey = x.d.Status,
Type = x.t == null ? "" : x.t.Value,
TypeKey = x.d.Type,
Row = index
};

请注意,如果不指定任何顺序,这可能会失败 - 或者只是给出不可靠的顺序。

关于c# - 如何将唯一的行号添加到我的 Linq 选择中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12025170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com