LINQ PropertyInfo out of range Exception when using List[i] but fine when declared variable tagged

34 views Asked by At
public List<string> ExcludeHeaders(IEnumerable<ARandomClass> query, List<string> Excludes)
    {
        var qu = query.FirstOrDefault().GetType().GetProperties().AsEnumerable();
        for (int y = 0; y < Excludes.Count; y++)
        {
            qu = qu.Where(c => c.Name != Excludes[y]);
        }

        return qu.Select(s => s.Name).ToList();
    }

This results in an error : Index out of Range

Whereas this works :

public List<string> ExcludeHeaders(IEnumerable<ARandomClass> query, List<string> Excludes)
    {
        var qu = query.FirstOrDefault().GetType().GetProperties().AsEnumerable();
        for (int y = 0; y < Excludes.Count; y++)
        {
            var str = Excludes[y];
            qu = qu.Where(c => c.Name != str);
        }

        return qu.Select(s => s.Name).ToList();
    }

losing my mind over this.

0

There are 0 answers