Reputation:
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,...
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 .
Upvotes: 1
Views: 85
Reputation: 4298
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());
Upvotes: 1
Reputation: 13146
In the Linq query, you are selecting anonymous complex type so you can't assign it a List<string>
. You should use var
to keep reference of anonymous type.
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.
Upvotes: 1