CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2012
    Location
    Nigeria
    Posts
    15

    Post 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
            }

  2. #2
    Join Date
    Mar 2012
    Posts
    17

    Re: Working with Generic List<T> collection and LINQ (Need Help)..

    Quote Originally Posted by chillaxzino View Post
    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

  3. #3
    Join Date
    Mar 2012
    Location
    Nigeria
    Posts
    15

    Post 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....

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured