Search code examples
c#listforeachxelement

Add to List from XElement in foreach loop


Program.cs

using ConsoleApp2.Models;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string City_Name = "Gdańsk";
            var doc = XElement.Load($"https://maps.googleapis.com/maps/api/place/textsearch/xml?key={key}&query=restaurants+in+{City_Name}&language=pl&fields=place_id");
            List<PlaceIdModel> IdList = doc.Descendants("result").Select(n => new PlaceIdModel
            {
                PlaceId = n.Element("place_id").Value

            }).ToList();
            List<PlaceDetailsModel> placeDetailsModel = new List<PlaceDetailsModel>();
            foreach (var item in IdList)
            {
                var doc2 = XElement.Load($"https://maps.googleapis.com/maps/api/place/details/xml?key={key}&place_id={item.PlaceId}&language=pl&fields=name,vicinity,geometry/location,rating,website,opening_hours/weekday_text");
                placeDetailsModel = doc2.Descendants("result").Select(x => new PlaceDetailsModel
                {
                    Name = x.Element("name").Value,
                    Vicinity = x.Element("vicinity").Value,
                    Rating = x.Element("rating").Value,
                    Website = x.Element("website").Value,
                    Lat = x.Element("geometry").Element("location").Element("lat").Value,
                    Lng = x.Element("geometry").Element("location").Element("lng").Value,
                    Opening_hours = x.Descendants("weekday_text").Select(y => y.Value).ToList()
                }).ToList();
            };
        }
    }
}

PlaceIdModel.cs

namespace ConsoleApp2.Models
{
    public class PlaceIdModel
    {
        public string PlaceId { get; set; }
    }
}

PlaceDetailsModel.cs

using System.Collections.Generic;

namespace ConsoleApp2.Models
{
    public class PlaceDetailsModel
    {
        public string Name { get; set; }
        public string Vicinity { get; set; }
        public string Rating { get; set; }
        public string Website { get; set; }
        public string Lat { get; set; }
        public string Lng { get; set; }
        public List<string> Opening_hours { get; set; }
    }
}

I'm using goole API places. First I get place_id from city then I want to use loop foreach to save details of every place into list. Everytime everything save on [0] instead populate whole list.


Solution

  • With .ToList() you are creating a new list every time. Instead add the items to the existing list with the List.AddRange(IEnumerable) Method.

    var placeDetailsModel = new List<PlaceDetailsModel>();
    foreach (var item in IdList)
    {
        var doc2 = XElement.Load($"https://maps.googleapis.com/maps/...");
        placeDetailsModel.AddRange(
            doc2.Descendants("result")
                .Select(x => new PlaceDetailsModel
                {
                    Name = x.Element("name").Value,
                    Vicinity = x.Element("vicinity").Value,
                    Rating = x.Element("rating").Value,
                    Website = x.Element("website").Value,
                    Lat = x.Element("geometry").Element("location").Element("lat").Value,
                    Lng = x.Element("geometry").Element("location").Element("lng").Value,
                    Opening_hours = x.Descendants("weekday_text").Select(y => y.Value).ToList()
                })
        );
    }
    

    You do not need the .ToList() when reading the IdList. The foreach works well with an IEnumerable<T> and it saves you an unnecessary memory allocation.:

    IEnumerable<PlaceIdModel> IdList = doc
        .Descendants("result")
        .Select(n => new PlaceIdModel
        {
            PlaceId = n.Element("place_id").Value
    
        });
    ...
    foreach (var item in IdList) ...
    

    I also do not see the advantage of having a PlaceIdModel class just to store the Id temporarily. Just use the values directly:

    var IdList = doc
        .Descendants("result")
        .Select(n => n.Element("place_id").Value);
    ...
    foreach (var id in IdList) {
        var doc2 = XElement.Load($"https://maps.googleapis.com/...&place_id={id}&language=...");
        ...
    }