[英]how to convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<>'.
I need to convert the following linq to a List directly,if I convert it to list I still get the error I explained on the subject,its said I need to convert it to list,but still the error is there,how can I convert it to list of string?without using additional conversion like foreach,... 我需要直接将以下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);
the first part of the above linq,i get the data and convert to list because I need to change my time stamp format,any help will be appreciated . 以上linq的第一部分,我获取数据并转换为列表,因为我需要更改时间戳格式,我们将不胜感激。
As you need to create list of comma separated values with header, 由于您需要创建带有标头的逗号分隔值列表,
//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());
In the Linq query, you are selecting anonymous complex type so you can't assign it a List<string>
. 在Linq查询中,您正在选择匿名复杂类型,因此您无法为其分配
List<string>
。 You should use var
to keep reference of anonymous type. 您应该使用
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();
Also, the ideal way is creating a class for complex type to handle reference instead of using anonymous type. 同样,理想的方法是为复杂类型创建一个类以处理引用,而不是使用匿名类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.