예제 #1
0
        /// <summary>
        /// 获取包含指定时间在内的前后共4条记录
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        List <FX_FinanceEconomicsModel> GetFirstLastTwo(List <FX_FinanceEconomicsModel> list)
        {
            List <FX_FinanceEconomicsModel> newList = new List <FX_FinanceEconomicsModel>();

            if (list != null && list.Count > 4)
            {
                int  count   = list.Count;
                long nowTime = DateExtensions.ConvertDateTimeInt(DateTime.Now);
                int  index   = 0;
                foreach (FX_FinanceEconomicsModel m in list)
                {
                    if (m.ReleasedDate >= nowTime)
                    {
                        break;
                    }
                    else
                    {
                        index++;
                    }
                }
                if (index == 0 || index == 1)
                {
                    newList = list.Take(4).OrderBy(x => x.ReleasedDate).ToList();
                }
                else if (index == 2)
                {
                    newList = list.Skip(1).Take(4).OrderBy(x => x.ReleasedDate).ToList();
                }
                else
                {
                    int addCount = 0;
                    for (int k = index; k < count; k++)
                    {
                        if (addCount < 2)
                        {
                            newList.Add(list[k]);
                            addCount++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    addCount = newList.Count;
                    for (int k = index - 1; k > -1; k--)
                    {
                        if (addCount < 4)
                        {
                            newList.Add(list[k - 1]);
                            addCount++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    newList = newList.OrderBy(x => x.ReleasedDate).ToList();
                }
                return(newList);
            }

            return(list);
        }