C# 如何从一个列表中的数据筛选另一个列表中的数据?

C# 如何从一个列表中的数据筛选另一个列表中的数据?,c#,C#,我的应用程序中有两个大小相同的列表。一个包含日期信息,另一个包含该日期的水电导率数据。我使用这两个列表在一个图表上绘制信息。我现在尝试添加一个滑块,允许用户按一定天数过滤数据。以下是我目前的代码: // Filter the date data (this works!) var filteredDates = from n in parsedDateList[0] where n >= beginDate.Date

我的应用程序中有两个大小相同的列表。一个包含日期信息,另一个包含该日期的水电导率数据。我使用这两个列表在一个图表上绘制信息。我现在尝试添加一个滑块,允许用户按一定天数过滤数据。以下是我目前的代码:

// Filter the date data (this works!)
var filteredDates = from n in parsedDateList[0]
                    where n >= beginDate.Date
                    select n;

//Filter the y-axis data (this does not work!)
var filteredCond = waterConductivityList[1].Where(x => parsedDateList[0].Any(y=> y.Date > beginDate));
有人能告诉我我做错了什么吗?y轴过滤器只是返回完整的信息列表,而不是进行过滤。

因此,我所做的是:

Dictionary<DateTime, int> conductivityData = new Dictionary<DateTime, int>();

// Get the y-values
i=0;
foreach (var entry in waterConductivityData[1])
            {
                condData[i] = Convert.ToInt32(entry);
                i++;
            }

// Add the dates ("entry" from datelist) and the y-value (condData) to the dictionary
i=0;
foreach (var entry in parsedDateList[0])
            {
                    conductivityData.Add(entry, condData[i]);
                    i++;
            }

//Add the data to the plot series using "key" for dates and "value" for y-data
foreach (var entry in conductivityData)
                    {
                            filteredDateStrings[0].Add(entry.Key.ToString("M/d/yy hh:mm tt"));
                            filteredCondData[0].Add(entry.Value);
                    }

// Update plot data
                i = 0;
                foreach (var entry in filteredCondData[0])
                {
                    waterSourceTwoChart.Series["conductivity"].Points.AddXY(filteredDateStrings[0].ElementAt(i), filteredCondData[0].ElementAt(i));
                    i++;
                }
Dictionary conductivityData=new Dictionary();
//获取y值
i=0;
foreach(水传导数据[1]中的var条目)
{
condData[i]=转换为32(条目);
i++;
}
//将日期(“日期列表中的条目”)和y值(condData)添加到字典中
i=0;
foreach(parsedDateList[0]中的var条目)
{
conductivityData.Add(条目,ConductData[i]);
i++;
}
//使用日期的“键”和y数据的“值”将数据添加到绘图系列中
foreach(电导数据中的var条目)
{
filteredDateStrings[0]。添加(entry.Key.ToString(“M/d/yy hh:mm tt”);
filteredCondData[0]。添加(entry.Value);
}
//更新绘图数据
i=0;
foreach(filteredCondData[0]中的变量项)
{
waterSourceTwoChart.Series[“conductivity”].Points.AddXY(FilteredDataStrings[0]。ElementAt(i),filteredCondData[0]。ElementAt(i));
i++;
}

谢谢大家的帮助

你为什么不使用
字典
?我不明白这是怎么回事。。。有人能进一步解释一下吗?同意蒂姆的建议。然而,为了解释您的经历:查看filteredCond的Where子句。您可以从Where子句中取出部分“parsedDateList[0]。Any(y=>y.Date>beginDate),如
bool b=parsedDateList[0]。Any(y=>y.Date>beginDate)。所以Where子句相当于
Where(x=>b)
。关于Where子句,
b
总是true或false——它在Where子句中不会改变,而与x无关。你看到了吗?@JohnAugust:你有两个列表,它们在逻辑上属于一起,因为每天都有一个
水导电性
。由于这一天也是不确定的,您可以使用日期为键的
字典
(使用
DateTime.date
截断时间)和
导水率
作为值。然后你只需要这样查找它:
waterconductivityfortoday=dateWaterConductivityMapping[DateTime.Today].Tim,谢谢你的解释,我要试试看。非常感谢。