how do access previous item in list using linQ?

后端 未结 5 690
南笙
南笙 2020-12-14 03:24

I have:

List A  = new List(){1,2,3,4,5,6};

List m=new List();
for(int i=1;i

        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 03:27

    Ok so getting the Next item in the list you can use:

    A.SkipWhile(x => x != value).Skip(1).FirstOrDefault();
    

    So to get the previous item use:

    var B = A.ToList();
    B.Reverse();
    B.SkipWhile(x => x != value).Skip(1).FirstOrDefault();
    

提交回复
热议问题