无法将类型'object'隐式转换为'System.Collections.Generic.List <float>'

时间:2018-02-23 15:40:22

标签: c# linq

嘿伙计们我是c#和LINQ的新手,我遇到了以下问题:

我从postgressql获得以下查询:

DataTable dt_production = new DataTable();

NpgsqlDataAdapter SumOfProduction = 
    new NpgsqlDataAdapter(@"select round(avg(m_corrected_av), 2)availability
                            from wpv.v_statistics_daily v1         
                            where   g.user_name in('" + Managers + @"')                                
                            group by 2
                            order by 1 desc 
                            limit 20", cn);         
SumOfProduction.Fill(dt_production);

我有以下课程:

public class ProductionAvalability
{
    public List<float> Avalability { get; set; }
    public ProductionAvalability()
    {          
        Avalability = new List<float>();

    }
}

现在我正在寻找一种方法来使用LINQ迭代我的数据表,如下所示,但我得到了我在主题中写的错误:

var SumOfProductionToList = (from DataRow dr in dt_production.Rows
                             select new ProductionAvalability
                             {
                                 Avalability= dr["average"]                                                                                                               
                             })
                             .ToList();
}).ToList();

我也尝试用(浮点数)抛出它并且仍然出现错误,它说无法将float转换为generic.list任何想法?如果我想使用LINQ?

1 个答案:

答案 0 :(得分:0)

试试这个:

List<float> list = (from DataRow dr in dt_production.Rows
                    select (float)dr["average"]).ToList();