System.Collections.Generic.IEnumerable' does not contain

本文关键字:does not contain Collections Generic IEnumerable System | 更新日期: 2023-09-27 18:13:09

我得到以下错误信息:

"Error  1   'System.Collections.Generic.IEnumerable<WindowsFormsApplication1.Form1.HRData>' does not contain a definition for 'CalculateInt32' and no extension method 'CalculateInt32' accepting a first argument of type 'System.Collections.Generic.IEnumerable<WindowsFormsApplication1.Form1.HRData>' could be found (are you missing a using directive or an assembly reference?)"

我不知道我做错了什么,这里是我的代码的一个例子,它涉及到:

public class HRData
    {
        public int? HeartRate
        {
            get;
            set;
        }
        public int? Speed
        {
            get;
            set;
        }
        public int? Power
        {
            get;
            set;
        }
        public int? Altitude
        {
            get;
            set;
        }
        public override string ToString()
        {
            return String.Format("Heart rate={0}, Speed={1}, Power={2}, Altitude={3}", HeartRate, Speed, Power, Altitude);
        }
    }    
public static class HRDataExtensions
    {
        static private int? CalculateInt32(this IEnumerable<HRData> data, Func<HRData, int?> valueSelector, Func<IEnumerable<int?>, int?> aggregation)
        {
            List<int?> list = new List<int?>();
            list.AddRange(data.Select(valueSelector));
            return aggregation(list);
        }
        static private int? CalculateDouble(this IEnumerable<HRData> data, Func<HRData, int?> valueSelector, Func<IEnumerable<int?>, double?> aggregation)
        {
            List<int?> list = new List<int?>();
            list.AddRange(data.Select(valueSelector));
            double? result = aggregation(list);
            return (result == null) ? null : (int?)Math.Round(result.Value);
        }
        static public int? MinimumHeartRate(this IEnumerable<HRData> data)
        {
            return data.CalculateInt32(hr => hr.HeartRate, Enumerable.Min);
        }
        static public int? MaximumHeartRate(this IEnumerable<HRData> data)
        {
            return data.CalculateInt32(hr => hr.HeartRate, Enumerable.Max);
        }
        static public int? AverageHeartRate(this IEnumerable<HRData> data)
        {
            return data.CalculateDouble(hr => hr.HeartRate, Enumerable.Average);
        }
        static public int? MinimumSpeed(this IEnumerable<HRData> data)
        {
            return data.CalculateInt32(hr => hr.Speed, Enumerable.Min);
        }
        static public int? MaximumSpeed(this IEnumerable<HRData> data)
        {
            return data.CalculateInt32(hr => hr.Speed, Enumerable.Max);
        }
        static public int? AverageSpeed(this IEnumerable<HRData> data)
        {
            return data.CalculateDouble(hr => hr.Speed, Enumerable.Average);
        }
        static public int? MinimumPower(this IEnumerable<HRData> data)
        {
            return data.CalculateInt32(hr => hr.Power, Enumerable.Min);
        }
        static public int? MaximumPower(this IEnumerable<HRData> data)
        {
            return data.CalculateInt32(hr => hr.Power, Enumerable.Max);
        }
        static public int? AveragePower(this IEnumerable<HRData> data)
        {
            return data.CalculateDouble(hr => hr.Power, Enumerable.Average);
        }
        static public int? MinimumAltitude(this IEnumerable<HRData> data)
        {
            return data.CalculateInt32(hr => hr.Altitude, Enumerable.Min);
        }
        static public int? MaximumAltitude(this IEnumerable<HRData> data)
        {
            return data.CalculateInt32(hr => hr.Altitude, Enumerable.Max);
        }
        static public int? AverageAltitude(this IEnumerable<HRData> data)
        {
            return data.CalculateDouble(hr => hr.Altitude, Enumerable.Average);
        }
    }
    public static class HRDataReader
    {
        static private int? ConvertValue(string[] values, int index)
        {
            if (index >= values.Length)
                return null;
            int value;
            if (int.TryParse(values[index], out value))
                return value;
            return null;
        }
        static public IList<HRData> Read(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException("fileName");
            using (StreamReader sr = new StreamReader(fileName))
            {
                string line;
                // First: Skip to the correct section.
                while ((line = sr.ReadLine()) != null)
                    if (line == "[HRData]")
                        break;
                // Now: Read the HRData
                List<HRData> data = new List<HRData>();
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.StartsWith("[") && line.EndsWith("]"))
                        break;
                    line = line.Trim().Replace("'t", " "); // Remove all tabs.
                    while (line.Contains("  ")) // Remove all duplicate spaces.
                        line = line.Replace("  ", " ");
                    string[] values = line.Split(' '); // Split the line up.
                    data.Add(new HRData
                    {
                        HeartRate = ConvertValue(values, 0),
                        Speed = ConvertValue(values, 1),
                        Power = ConvertValue(values, 2),
                        Altitude = ConvertValue(values, 3)
                    });
                }
                return data;
            }
        }
    }
这是我的代码的第二部分,希望能有所帮助。它得到了所有的输出等
private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            IList<HRData> data = HRDataReader.Read(openFileDialog1.FileName);
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "HeartRate", HeaderText = "Heart rate", DataPropertyName = "HeartRate" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Speed", HeaderText = "Speed", DataPropertyName = "Speed" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Power", HeaderText = "Power", DataPropertyName = "Power" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Altitude", HeaderText = "Altitude", DataPropertyName = "Altitude" });
            dataGridView1.DataSource = data;
            label1.Text = data.MaximumAltitude().ToString();
            textBox1.Text = data.MaximumSpeed().ToString();
            textBox2.Text = data.AverageSpeed().ToString();
            textBox3.Text = data.AverageHeartRate().ToString();
            textBox4.Text = data.MaximumHeartRate().ToString();
            textBox5.Text = data.MinimumHeartRate().ToString();
            textBox6.Text = data.AveragePower().ToString();
            textBox7.Text = data.MaximumPower().ToString();
            textBox8.Text = data.AverageAltitude().ToString();
            textBox9.Text = data.MaximumAltitude().ToString();
        }
    }

System.Collections.Generic.IEnumerable' does not contain

您的前两个扩展方法目前是私有的。你是想把它们公之于众吗?

假设错误是在另一个试图调用CalculateInt32的类中-从您的问题中不清楚。

此外,您根本不清楚为什么要在CalculateInt32中创建列表。为什么不直接使用:

private static int? CalculateInt32(this IEnumerable<HRData> data,
                                   Func<HRData, int?> valueSelector,
                                   Func<IEnumerable<int?>, int?> aggregation)
{
    return aggregation(data.Select(valueSelector));
}

(顺便说一句,我通常在静态修饰符之前看到访问修饰符。两者都是有效的,但public staticstatic public更常见。)

编辑:此外,我看到你的错误信息是谈论WindowsFormsApplication1.Form1.HRData -是否有可能,你有两个HRData类型,一个嵌套的一个和一个非嵌套的?