I have the following piece of code:
public List<myTask> MyTask { get; set; }
public class myTask
{
public List<int> ID { get; set; }
public List<DateTime> Date { get; set; }
public Decimal Total { get; set; }
}
List<myTask> tempTask = new List<myTask>();
MyTask = new List<myTask>();
I want to do something as follows but having the error message:
MyTask = tempTask.GroupBy(x => x.Date);
Error 1 Cannot implicitly convert type
‘System.Collections.Generic.IEnumerable,MvcUI.Models.MyTask>>’
to ‘System.Collections.Generic.List’. An explicit
conversion exists (are you missing a cast?)
Edited Code:
I have tried to add the code:
var newtempTask = tempTask.GroupBy(x => x.Date, (Date, values) =>
new
{
ID = values.Select(x => x.ID).ToList(),
Date = values.Select(x => x.Date).ToList(), Total=values.Select(x => x.Date).ToList()
}); and then
foreach (var newitem in newtempTask)
{
MyTask.Add(new MyTask
{
ID = newitem.ID,
Date= newitem.Date,Total = newitem.Total
});}
I have tried to add the code, please see the above edited code. I am having the error message, Error 7 Cannot implicitly convert type ‘System.Collections.Generic.List>’ to ‘System.Collections.Generic.List’
You lack explanations on what you are trying to achieve…
I think the following could be what you are asking for:
(it selects the first task in each group by date)
Or do you perhaps want new tasks for each date with the IDs of all tasks in that group and the sum of the totals?
In that case the query is a little more complex: