Return every item in a list from a foreach

18,081

You have to return a new list, in your example you're just setting name to the current name then returning name at the end. Since you loop through all the elements name is set to the final value in the list before returning. Besides that, your return type is string unless you concatinate all the elements in li into a single string then you can't return a list as a string.

 List<string> newList = li.Select(x => x["title"].ToString()).ToList();

Will create a new list of strings where each element is the title of an element from your source list, li.

If you really want to return a string you can use (I believe) String.Join or, (this one I'm certain of) Aggregate like so;

 return li.Aggregate((c, n) => c["title"].ToString() + ", " + b["title"].ToString());

The above code will return string that is a comma separated list of the elements titles.

Share:
18,081
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years
    List<object> li = new List <object>(Items);
    string name = "";
    foreach (var item in Items)
    {
        name = item["title"].ToString();
    }
    return name;
    

    Using this code snippet, I can't find a way for to change the return to output all items from the list. As is, it only returns the last item. How can I get every item returned?

  • O. R. Mapper
    O. R. Mapper over 10 years
    yield return and returnwithin one method? If I remember correctly, this will not compile.
  • DotNetRussell
    DotNetRussell over 10 years
    Yeah I fixed it thanks I was thinking that it would complain about not all paths exicuting a return
  • Dustin Kingen
    Dustin Kingen over 10 years
    Your code will not compile. Assignment cannot happen in the yield return
  • Chris
    Chris over 10 years
    Also if this is not your recommended solution why not include the recommended solution that returns the whole thing at once?
  • DotNetRussell
    DotNetRussell over 10 years
    @Chris because he didn't ask for my opinion he asked how to return one thing at a time
  • Chris
    Chris over 10 years
    He wanted to return each item in the list. You're basically returning an ienumerable at the moment. If you think returning a list is better (for example) then that still qualifies as returning each item in the list...
  • O. R. Mapper
    O. R. Mapper over 10 years
    Given that the OP did not seem to expect to only get back the last item based on his or her code, I fear he or she may not understand concepts such as the yield return-compiler magic iterators created by the C# compiler and how to use them without any further explanations yet. BTW, what purpose does the name variable serve in your code?
  • DotNetRussell
    DotNetRussell over 10 years
    lol To everyone that has a problem with this answer... As I understood the question, the OP wanted to do a return of each item from a foreach loop. I answered that question. Its not a big deal, relax lol...