【问题标题】:problemin adding index field to linq results将索引字段添加到 linq 结果的问题
【发布时间】:2018-01-26 10:08:25
【问题描述】:

我的记录显示需要序列号。每页 50 条记录。

    public List<string[]> InstructionsData(IEnumerable<Assets> InstructionsEntry, int currentUserId)
    {

        return InstructionsEntry.Select((entry,index) => new string[]
        {                  
           (index + 1).ToString(),
            entry.state_Id,                
            entry.batchRef_Id,
            entry.assetCategory_Id,                
            GetAge(entry.age),                
            entry.assetStatus_Id,
            GetStatusTag(entry.recordStatus ??false),
            entry.availbaleQty.ToString(),
            entry.createdBy,

        }).ToList();

上面用于显示索引的代码工作正常。我的问题是当我移动到下一页时,索引再次从第一页开始。请帮我在下一页继续索引号。

【问题讨论】:

  • 您将不得不使用 linq skip 和 take 命令,但您的函数结构错误,因为它不使用索引进行分页。尝试重新格式化您的问题和方法。

标签: c# linq


【解决方案1】:

简单:

public List<string[]> InstructionsData(IEnumerable<Assets> InstructionsEntry, int currentUserId, int startIndex)
    {

        return InstructionsEntry.Select((entry,index) => new string[]
        {                  
           (startIndex + index + 1).ToString(),
            entry.state_Id,                
            entry.batchRef_Id,
            entry.assetCategory_Id,                
            GetAge(entry.age),                
            entry.assetStatus_Id,
            GetStatusTag(entry.recordStatus ??false),
            entry.availbaleQty.ToString(),
            entry.createdBy,

        }).ToList();

【讨论】:

    【解决方案2】:

    我不会像

    那样争论你的设计选择
    • 为什么需要返回字符串数组而不是结构化对象?
    • 或者当你必须有一个唯一的标识符来依赖时,为什么你需要索引?

    您可能在幕后有您非常特殊的具体原因,但我会尝试指出我最终可能会做什么来检索分页索引结果集。

    首先,获取资产的分页结果集(或您的用例场景需要的任何内容),包含有关给定页面的一些元数据(当前页码、页面大小、总记录)

    var paged = data.Page(page: 2, size: 5);
    

    然后在前端或要显示结果的任何地方,将索引附加到返回的记录(我将展示 C# 等效项,但您将在您选择的技术堆栈中轻松获得相同的结果)

    var indexed = paged.Items
        .Select((current, index) =>
        {
            var skipped = (paged.Page - 1) * paged.Size;
            return new
            {
                Index = skipped + index,
                Item = current,
            };
        });
    

    分页结果如下所示

    public class Paged<T>
    {
        public IEnumerable<T> Items { get; set; }
        public int Page { get; set; }
        public int Size { get; set; }
        public int Total { get; set; }
    }
    
    public class Asset
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    而实际的分页机制仅适用于SkipTake

    public static class Extensions
    {
        public static Paged<T> Page<T>(this IEnumerable<T> instance, int? page = 1, int? size = 10)
        {
            var items = instance
                .Skip((page.Value - 1) * size.Value)
                .Take(size.Value)
                .ToList();
    
            return new Paged<T>()
            {
                Page = page.Value,
                Size = size.Value,
                Total = instance.Count() - 1,
                Items = items,
            };
        }
    }
    

    查看gist 以获取概览代码示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多