【问题标题】:Get index of selected Enumerable items获取选定的可枚举项的索引
【发布时间】:2017-04-12 16:32:40
【问题描述】:
public IEnumerable<InvalidSimContract> ValidateSims(SimSearchCriteriaContract searchCriteria)
        {
            var retval = new List<InvalidSimContract>();
            VZWSuspendLogic obj = new VZWSuspendLogic();
             var allowedSuspendDaysExpiredSims = searchCriteria.UserInputSims
                .Where(s => obj.HasExpiredAllowedSuspendDays(s.SimId, searchCriteria.ServiceTypeId, searchCriteria.ToState.Id))
                .Select(s => s.SimNumber).ToList();

            if (allowedSuspendDaysExpiredSims != null)
            {
                return allowedSuspendDaysExpiredSims.Select((s, i) =>
                new InvalidSimContract
                {
                    Message = String.Format("Line {0} contains SIM Number :{1} has expired the maximum allowed suspension days for this year. Allowed suspension days for the year is {2} days.", i + 1,s, _allowedSuspendDaysInLast12Months),
                    UserInput = s,
                    ImeiNumber = string.Empty,
                    LineNumber = i + 1
                }
            ).ToList();
            }

            return retval;
        }

我想将过滤项目的索引号打印为行号。那么如何获取被选中项的过滤索引号呢。

【问题讨论】:

  • 我不明白。您想要UserInputSims 集合中过滤项目的索引吗?
  • 很难理解你的问题。也许您可以将代码简化为一些最小的示例。

标签: c# lambda


【解决方案1】:

您必须在执行.Where() 之前添加i(索引)

var allowedSuspendDaysExpiredSims = searchCriteria.UserInputSims
    .Select((s, i) => new
    {
        Obj = s,
        Ix = i,
    })
    .Where(s => obj.HasExpiredAllowedSuspendDays(s.Obj.SimId, searchCriteria.ServiceTypeId, searchCriteria.ToState.Id))
    .Select(s => new
    {
        s.Obj.SimNumber,
        s.Ix,
    }).ToList();

if (allowedSuspendDaysExpiredSims != null)
{
    return allowedSuspendDaysExpiredSims.Select(s =>
    new InvalidSimContract
    {
        Message = String.Format("Line {0} contains SIM Number :{1} has expired the maximum allowed suspension days for this year. Allowed suspension days for the year is {2} days.", s.Ix + 1, s.SimNumber, _allowedSuspendDaysInLast12Months),
        UserInput = s.SimNumber,
        ImeiNumber = string.Empty,
        LineNumber = s.Ix + 1
    }
    ).ToList();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    相关资源
    最近更新 更多