Linq ForEach - Returning cannot assign 'void' to implicitly typed local variable
I am trying to use linq query to save multiple lines of code. I am getting compilation error when I am told:
Returning cannot assign 'void' to an implicitly typed local variable.
var GIANTLIST = new List<string>();
var taskIds = Complaint.Tasks.Select(s => s.Task_ID).ToList().ForEach( s =>
{
GIANTLIST.Add("<Task_ID=" + s.ToString() + ">");
});
I am trying to understand the linq query better. I understand it got a "void" return type? If so, how can I then add to the list?
source to share
Foreach returns no result; you cannot assign it to a variable. Remove var taskIds:
var GIANTLIST = new List<string>();
Complaint.Tasks.Select(s => s.Task_ID).ToList().ForEach( s =>
{
GIANTLIST.Add("<Task_ID=" + s.ToString() + ">");
});
You have Microsoftsoft documentation here https://msdn.microsoft.com/en-us/library/bwabdf9z%28v=vs.110%29.aspx
I am trying to understand the linq query better. I understand this is getting an "invalid" return type? If so, how can I then add to the list?
Think of it as a ForEach like a normal functional ForEach ForEach (var a in MyList) that it doesn't return anything is also invalid. Inside ForEach, you can directly modify the variables of your classes.
source to share
that ForEach is not part of Linq, it is a method of the List class
keep it simple
var GIANTLIST = Complaint.Tasks.Select(s => "<Task_ID=" + s.Task_ID + ">").ToList();
if you need to add items to an existing list use AddRange instead of ForEach
GIANTLIST.AddRange(Complaint.Tasks.Select(s => "<Task_ID=" + s.Task_ID + ">"));
source to share
The ForEach method returns void. You are assigning to a void
value taskIds
. If you want to fill in taskIds, do the following:
var GIANTLIST = new List<string>();
var taskIds = new List<int>();
Complaint.Tasks.Select(s => s.Task_ID).ToList().ForEach( s =>
{
taskIds.Add(s.TASK_ID);
GIANTLIST.Add("<Task_ID=" + s.ToString() + ">");
});
source to share
Your attempt to do two things at the same time. Selecting a Task_ID to a list and adding each Task_ID to a different list.
You can do this in two steps
var GIANTLIST = new List<string>();
var taskIds = Complaint.Tasks.Select(s => s.Task_ID).ToList();
taskIds.ForEach(s =>
{
GIANTLIST.Add("<Task_ID=" + s.ToString() + ">");
});
or pre-initialize both lists in advance and add values ββto ForEach
var GIANTLIST = new List<string>();
var taskIds = new List<int>();
Complaint.Tasks.Select(s => s.Task_ID).ForEach(s =>
{
taskIds.Add(s);
GIANTLIST.Add("<Task_ID=" + s.ToString() + ">");
});
ForEach itself doesn't return anything, it just performs the specified action for each list object.
source to share
The following should work:
var GIANTLIST = new List<string>();
Complaint.Tasks.Select(s => s.Task_ID).ForEach( s =>
{
GIANTLIST.Add("<Task_ID=" + s.ToString() + ">");
});
or
var GIANTLIST = new List<string>();
var taskIds = Complaint.Tasks.Select(s => s.Task_ID)
taskIds.ForEach( s =>
{
GIANTLIST.Add("<Task_ID=" + s.ToString() + ">");
});
You don't actually need to convert ToList()
, ForEach can be called on the base class IEnumerable
source to share