我正在尝试获取列表的索引,然后减去1以获取先前的值。
thursday = LastThursdayOfTheMonth(j, i);
if(LStock.Any(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2)))
{
object line = LStock.First(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2));
LFridays.Add(line);
}
我想在列表中添加上一个元素。我该怎么办?
谢谢!
已解决
if(LStock.Any(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2)))
{
line = LStock.First(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2));
int index = LStock.IndexOf(line) - 1;
LFridays.Add(LStock.ElementAt(index));
}
答案 0 :(得分:0)
您尚未提供可编译的代码,因此如果不作大量补充和可能的假设,就无法验证这一点,但这应该可行。
var line = LStock.FirstOrDefault(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2));
if (line != null)
{
int index = LStock.IndexOf(line) - 1;
LFridays.Add(LStock.ToList().ElementAt(index));
}
如果LStock
已经是List
,则可以省略.ToList()
。
答案 1 :(得分:0)
尝试这个:
var find = thursday.ToString("dd-MMM-yyyy", dtf2);
var current =
LStock
.Skip(1)
.Zip(LStock, (n, c) => new { next = n.date, current = c.date })
.First(x => x.next == find)
.current;