简体   繁体   English

如何转换类型&#39;System.Collections.Generic.IEnumerable <AnonymousType#1> &#39;到&#39;System.Collections.Generic.IEnumerable &lt;&gt;&#39;。

[英]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.

相关问题 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.IEnumerable - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable 无法隐式转换类型System.Collections.Generic.Ienumerable <string> 到system.Linq.IOrderedEnumerable <AnonymousType#1> - can not implicitly convert type System.Collections.Generic.Ienumerable<string> to system.Linq.IOrderedEnumerable<AnonymousType#1> 无法将类型'System.Collections.Generic.IEnumerable <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <string> - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<string> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;to&#39;System.Collections.Generic.IEnumerable <Models> - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<Models> 参数1:无法从&#39;System.Collections.Generic.IEnumerable转换 <AnonymousType#1> &#39;到&#39;int&#39; - Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'int' Linq-无法隐式转换类型&#39;System.Collections.Generic.IEnumerable - Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable 无法将类型System.Collections.Generic.IEnumerable &lt;&gt;隐式转换为bool - Cannot implicitly convert type System.Collections.Generic.IEnumerable<> to bool 无法从“ System.Collections.Generic.IEnumerable”转换为System.Collections.Generic.IEnumerable <string> - cannot convert from 'System.Collections.Generic.IEnumerable' to System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换为System.Collections.Generic.IEnumerable <string> - Cannot convert from 'System.Collections.Generic.IEnumerable to System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换 <int> &#39;到&#39;System.Collections.Generic.IEnumerable <int?> “ - Cannot convert from 'System.Collections.Generic.IEnumerable<int>' to 'System.Collections.Generic.IEnumerable<int?>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM