Get index of lines that match my linq

前端 未结 3 1644
我寻月下人不归
我寻月下人不归 2021-01-25 21:45

I have a linq statement and I would like to know if it is possible to get indicies of lines that match my statement? Here it is:

var result = list3.Where(middle          


        
3条回答
  •  -上瘾入骨i
    2021-01-25 22:40

    You can use the overload of Select (or Where) which projects also the index of the element:

    var result = list3.Select((middle, index) => new{ middle, index })
        .Where(x => list4.Any(xx => xx == x.middle.Middle.category1))
        .Select(x => new { x.middle, x.index, dt = DateTime.ParseExact(x.middle.LeftColumn, dateFormat, CultureInfo.InvariantCulture) })
        .Where(x => x.dt >= czas11 && x.dt <= czas22)
        .Select(x => x.index)
        .ToList();
    

    Side-note: consider to change your variable names to be more meaningful. That is unreadable.

提交回复
热议问题