0
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;
        }

I want to print the index number of the filtered item as a line number. So how to get filter the index number too of the selected item.

Jobi
  • 1,102
  • 5
  • 24
  • 38
  • 1
    Possible duplicate of [IEnumerable.Select with index](http://stackoverflow.com/questions/27285061/ienumerable-select-with-index) – MiGro Apr 12 '17 at 08:28
  • I don't understand. You want the index of the filtered itemes inside the `UserInputSims` collection? – Pikoh Apr 12 '17 at 08:29
  • It's hard to understand your question. Maybe you could reduce the code to some minimal example. – vyrp Apr 12 '17 at 08:31

1 Answers1

2

You have to add the i (index) before doing the .Where():

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();
}
xanatos
  • 109,618
  • 12
  • 197
  • 280