如何将类型'System.Collections.Generic.IEnumerable <anonymoustype#1>'转换为'System.Collections.Generic.IEnumerable&lt;&gt;'。

时间:2017-12-17 15:28:17

标签: c# linq

我需要将以下linq直接转换为List,如果我将其转换为list我仍然得到我在该主题上解释的错误,它说我需要将其转换为list,但仍然是错误,那么我可以将它转换为字符串列表吗?不使用像foreach这样的额外转换,...​​

List<string>  eventResult= (from c in DB.Events
                        where (c.m_turbine_id == turbineid.turbineID) && (c.m_time_stamp >= frmDate && c.m_time_stamp <= toDate)

                 select new EventLogPartialViewModel
                 {
                     Timestamp = c.m_time_stamp,
                     Description = c.m_event_log_description,
                     WindSpeed = c.m_wind_speed,
                     RPM = c.m_rpm,
                     Power = c.m_power
                 }).ToList().Select(x => new
                           {
                               Timestamp = x.Timestamp.ToString("dd/MM/yyyy H:mm:ss"),
                               Description = x.Description,
                               WindSpeed = x.WindSpeed,
                               RPM = x.RPM,
                               Power = x.Power
                           }).ToList().OrderByDescending(m => m.Timestamp);

上面的linq的第一部分,我得到数据并转换为列表,因为我需要更改我的时间戳格式,任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

因为您需要使用标题

创建逗号分隔值列表
//Add headers as first item
List<string>  eventResult = new List<string>(){"Timestamp,Description,WindSpeed,RPM,Power"};

//Add records
eventResult.AddRange(from c in DB.Events
                        where (c.m_turbine_id == turbineid.turbineID) && (c.m_time_stamp >= frmDate && c.m_time_stamp <= toDate)

                 select new EventLogPartialViewModel
                 {
                     Timestamp = c.m_time_stamp,
                     Description = c.m_event_log_description,
                     WindSpeed = c.m_wind_speed,
                     RPM = c.m_rpm,
                     Power = c.m_power
                 }).ToList().Select(x => 
                      x.Timestamp.ToString("dd/MM/yyyy H:mm:ss")) + "," +
                      x.Description + "," +
                      x.WindSpeed + "," +
                      x.RPM + "," +
                      x.Power

                 .ToList());

答案 1 :(得分:1)

在Linq查询中,您选择匿名复杂类型,因此无法为其分配List<string>。您应该使用var来保留匿名类型的引用。

var eventResult= (from c in DB.Events
                        where (c.m_turbine_id == turbineid.turbineID) && (c.m_time_stamp >= frmDate && c.m_time_stamp <= toDate)

                 select new EventLogPartialViewModel
                 {
                     Timestamp = c.m_time_stamp,
                     Description = c.m_event_log_description,
                     WindSpeed = c.m_wind_speed,
                     RPM = c.m_rpm,
                     Power = c.m_power
                 }).ToList().Select(x => new
                           {
                               Timestamp = x.Timestamp.ToString("dd/MM/yyyy H:mm:ss"),
                               Description = x.Description,
                               WindSpeed = x.WindSpeed,
                               RPM = x.RPM,
                               Power = x.Power
                           }).OrderByDescending(m => m.Timestamp).ToList();

另外,理想的方法是为复杂类型创建一个类来处理引用而不是使用匿名类型。

相关问题