none
Getting a list of ids from a list within a list RRS feed

  • Question

  • Hi,

    I have a list of Bets and within each Bet I have another list of possible Results.  Each result has an Id.  I want to get a comma seperated list of result ids using linq ..something like

    string.join(",", Bets.Select(x => x.Results.Select(y => y.Id)).ToList())

    but thats not working..can anyone offer any advice on way to do this? a preferably a nice and efficient way..

    thanks for your time

    Tuesday, February 25, 2014 4:00 AM

Answers

  • Hello,

    It needs us to use foreach:

    List<int> ids = new List<int>();
    
    
    
                    foreach (Order order in result)
    
                    {
    
                        foreach (OrderDetail od in order.OrderDetails)
    
                        {
    
                            ids.Add(od.OrderDetailID);
    
                        }
    
                    }
    

    With LINQ, we cannot return second level type property directly.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 26, 2014 2:18 AM
    Moderator
  • Hi jameswoodmancy;

    Something like this should give you what you need.

    var results = Bets.Select(b => b.Results
                      .Aggregate("", (currentIds, nextId) => currentIds + nextId.Id + ","))
                      .ToList();
    
    // Remove the last comma on each list
    for (int i = 0; i < results.Count; i++)
    {
        results[i] = results[i].TrimEnd(new Char[] { ',' });
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Sunday, March 2, 2014 4:31 PM

All replies

  • Hello,

    It needs us to use foreach:

    List<int> ids = new List<int>();
    
    
    
                    foreach (Order order in result)
    
                    {
    
                        foreach (OrderDetail od in order.OrderDetails)
    
                        {
    
                            ids.Add(od.OrderDetailID);
    
                        }
    
                    }
    

    With LINQ, we cannot return second level type property directly.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 26, 2014 2:18 AM
    Moderator
  • Hi jameswoodmancy;

    Something like this should give you what you need.

    var results = Bets.Select(b => b.Results
                      .Aggregate("", (currentIds, nextId) => currentIds + nextId.Id + ","))
                      .ToList();
    
    // Remove the last comma on each list
    for (int i = 0; i < results.Count; i++)
    {
        results[i] = results[i].TrimEnd(new Char[] { ',' });
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Sunday, March 2, 2014 4:31 PM