在c#

时间:2017-07-13 17:07:32

标签: c#

我有两个列表 - StockData和UpDownData。

db1 newStock = new db1();
newStock.Date = (DateTime)reader["Date"];
newStock.High = (double)reader["High"];
StockData.Add(newStock);

db1 newStock2 = new db1();
newStock2.Date = (DateTime)reader2["Date"];
newStock2.Min_Max_Value = (double)reader2["Min_Max_Value"];
UpDownData.Add(newStock2);

int a = 0;
int pos = 0;
DateTime? date1 = null;

while (a < UpDownData.Count && UpDownData.ElementAt(a)?.Date == StockData[i].Date)
{
pos = a;                            
date1 = UpDownData.ElementAt(a).Date;
a++;
}
Console.WriteLine(date1);

我正在尝试将 StockData 列表中的日期与 UpDownData 列表中的日期进行匹配。找到匹配后,我使用UpDownData列表中元素的位置 a )来打印匹配的日期

但是上面的代码无法找到匹配的日期。有人可以帮忙吗?

由于

编辑:

下面的图片可以给你一个想法:

enter image description here

因此,UpDownData List中的匹配日期分别为6/30/2015和7/16/2015,分别位于a = 0和a = 6。我只需要POSTION和DATES。

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication65
{
    class Program
    {
        static void Main(string[] args)
        {
            StockData.data = new List<StockData>() {
                new StockData() { date = DateTime.Parse("6/17/2015"), high = 256.9},
                new StockData() { date = DateTime.Parse("6/18/2015"), high = 260.7},
                new StockData() { date = DateTime.Parse("6/23/2015"), high = 271.2},
                new StockData() { date = DateTime.Parse("6/24/2015"), high = 267.9},
                new StockData() { date = DateTime.Parse("6/25/2015"), high = 266},
                new StockData() { date = DateTime.Parse("6/26/2015"), high = 266.9},
                new StockData() { date = DateTime.Parse("6/30/2015"), high = 263.35},
                new StockData() { date = DateTime.Parse("7/8/2015"), high = 271},
                new StockData() { date = DateTime.Parse("7/10/2015"), high = 271},
                new StockData() { date = DateTime.Parse("7/13/2015"), high = 274.35},
                new StockData() { date = DateTime.Parse("7/14/2015"), high = 273.6},
                new StockData() { date = DateTime.Parse("7/15/2015"), high = 271.7},
                new StockData() { date = DateTime.Parse("7/16/2015"), high = 272.75}
            };


            UpDownData.data = new List<UpDownData>() {
                new UpDownData() { date = DateTime.Parse("6/30/2015"), min_max = 263.35},
                new UpDownData() { date = DateTime.Parse("7/8/2015"), min_max = 250},
                new UpDownData() { date = DateTime.Parse("7/10/2015"), min_max = 236.65},
                new UpDownData() { date = DateTime.Parse("7/13/2015"), min_max = 223.3},
                new UpDownData() { date = DateTime.Parse("7/14/2015"), min_max = 209.95},
                new UpDownData() { date = DateTime.Parse("7/15/2015"), min_max = 196.6},
                new UpDownData() { date = DateTime.Parse("7/16/2015"), min_max = 272.75}
            };

            var results = (from sData in StockData.data
                           join uData in UpDownData.data on sData.date equals uData.date
                           select new { sData = sData, uData = uData })
                          .Where(x => x.sData.high == x.uData.min_max)
                          .Select(x => new { date = x.sData.date, value = x.sData.high }).ToList();

        }
    }
    public class StockData
    {
        public static List<StockData> data = new List<StockData>();

        public DateTime date { get; set; }
        public double high { get; set; }
    }
    public class UpDownData
    {
        public static List<UpDownData> data = new List<UpDownData>();

        public DateTime date { get; set; }
        public double min_max { get; set; }
    }
}