Working with Generic List<T> collection and LINQ (Need Help)..
I intend populating variable newOutput with my query result in the method below and return a newOutput to these method's caller. Please fix the population problem.
Code:
// public method that uses LINQ Query to reorder these class's lists
public List<string> Reordering(List<string> listToReorder)
{
//LINQ used to reorder listToReorder collections
var reordered =
from item in listToReorder
orderby item descending // arrange query in descending order
select item;
List<string> newOutput = new List<string>();
//These where my problem begins
foreach (var item in reordered)
{
for (int i = 0; i < reordered.Count(); i++)
{
newOutput[i] = item;
}
}
return newOutput; // return reordered to caller as List<string> type
}
Re: Working with Generic List<T> collection and LINQ (Need Help)..
Quote:
Originally Posted by
chillaxzino
I intend populating variable newOutput with my query result in the method below and return a newOutput to these method's caller. Please fix the population problem.
Code:
// public method that uses LINQ Query to reorder these class's lists
public List<string> Reordering(List<string> listToReorder)
{
//LINQ used to reorder listToReorder collections
var reordered =
from item in listToReorder
orderby item descending // arrange query in descending order
select item;
List<string> newOutput = new List<string>();
//These where my problem begins
foreach (var item in reordered)
{
for (int i = 0; i < reordered.Count(); i++)
{
newOutput[i] = item;
}
}
return newOutput; // return reordered to caller as List<string> type
}
try using this
foreach (var item in reordered)
{
newOutput.Items.add(item);
}
instead of
foreach (var item in reordered)
{
for (int i = 0; i < reordered.Count(); i++)
{
newOutput[i] = item;
}
}
why? => because if reordered has 5 items
means u got 5 loop in foreach and 5 loop in for
and when
at foreach first loop, the newOutput at index 0-4 will be same as [first item]
and at foreach second loop, the newoutput at index 0-4 will be same as [second item]
so if im not wrong. there will only 5 data at list [if reordered has 5 data]
and all of them will be the last item at reordered
and better to use list.Items.add(); [will add automatically without mention the index]
ps: sorry if my english was bad
Re: Working with Generic List<T> collection and LINQ (Need Help)..
Thanks for that wonderful contribution of yours... Here how I implemented that.
Code:
// public method that uses LINQ Query to reorder these class's lists
public List<string> Reordering(List<string> listToReorder)
{
//LINQ used to reorder listToReorder collections
var reordered =
from item in listToReorder
orderby item descending // arrange query in descending order
select item;
List<string> newOutput = new List<string>();
// These code was commented because it didn't work
//foreach (var item in reordered)
//{
// for (int i = 0; i < reordered.Count(); i++)
// {
// newOutput[i] = item;
// }
//}
//Here's the code that worked
foreach (var item in reordered)
{
newOutput.Add(item);
}
return newOutput; // return reordered to caller as List<string> type
}
Your suggestion was this:
Code:
foreach (var item in reordered)
{
newOutput.Items.Add(item);
}
And the correction I made to your code was this:
Code:
foreach (var item in reordered)
{
newOutput.Add(item);
}
Thanks so much....