在 foreach 循环中从 XElement 添加到列表

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

我正在使用 goole API 个地方。首先我从 city 得到 place_id 然后我想使用循环 foreach 将每个地方的详细信息保存到列表中。每次都保存在 [0] 上而不是填充整个列表。

使用 .ToList(),您每次都在创建一个新列表。而是使用 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()
            })
    );
}

阅读IdList时不需要.ToList()foreachIEnumerable<T> 配合使用效果很好,它可以为您节省不必要的内存分配。:

IEnumerable<PlaceIdModel> IdList = doc
    .Descendants("result")
    .Select(n => new PlaceIdModel
    {
        PlaceId = n.Element("place_id").Value

    });
...
foreach (var item in IdList) ...

我也没有看到 PlaceIdModel class 只是为了临时存储 Id 的优势。直接使用值:

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=...");
    ...
}