C# 循环访问嵌套的 LINQ select 语句

本文关键字:LINQ select 语句 嵌套 循环 访问 | 更新日期: 2023-09-27 18:33:44

下面的示例解析一个XML文档,然后循环访问成员并将它们存储在对象列表中(数据最终在SQL数据库中):

public static void Parse(XDocument xml)
{
    XNamespace ns = "http://somenamespace.com/ns";
    var Locations =
        from Continents in xml.Descendants(ns + "Continent")
        from Countries in Continents.Elements(ns + "Country")
        select new
        {
            Continent1 = (string) Continents.Element(ns + "Europe"),
            Country1 = (string) Countries.Element(ns + "United_Kingdom"),
            Cities = from Cities in Countries.Elements(ns + "City")
                select new
                {
                    City1 = (string) Cities.Element(ns + "London")
                }
        };
    List<Location> locationColl = new List<Location>();
    loc_Entity_FrameworkContainer context = new loc_Entity_FrameworkContainer();
    var i = 0;
    foreach (var location in Locations)
    {
        Location l = new Location();
        locationColl.Add(l);
        locationColl[i].Continent = (string) location.Continent1;
        locationColl[i].Country = (string) location.Country1;
        locationColl[i].City = (string) location.City1; // Can't access "City1"
        context.Location.Add(locationColl[i]);
        i++;
    }
    context.SaveChanges();
}

声明:locationColl[i].City = (string)location.City1; 找不到"城市 1"。(这就是问题所在,我无法在一个循环中从"位置"访问所有成员)

位置等级:

namespace locationProject
{
    using System;
    using System.Collections.Generic;
    public partial class Location
    {
        public string Continent { get; set; }
        public string Country { get; set; }
        public string City { get; set; }
    }
}

XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:ns="http://somenamespace.com/ns">
   <ns:Continent>
      <ns:Europe>21c99a56-4b3d-4571-802a-76cdb6b81a01</ns:Europe>
      <ns:Country>
         <ns:United_Kingdom>eb2e9eec-dc3b-4636-bcf5-dba0024e62f3</ns:United_Kingdom>
         <ns:City>
            <ns:London>109b48ec-d829-4a87-b200-4dc9a94db48c</ns:London>
         </ns:City>
      </ns:Country>
   </ns:Continent>
   <ns:Continent>
      <ns:Europe>a11ed925-dc0d-4dfd-b1c2-52eb697ad689</ns:Europe>
      <ns:Country>
         <ns:United_Kingdom>a61d02ef-7b80-4390-926a-49c6d9af9634</ns:United_Kingdom>
         <ns:City>
            <ns:London>dbb9c5cc-b08f-4223-b32c-acb4ed9ce97c</ns:London>
         </ns:City>
      </ns:Country>
   </ns:Continent>
</feed>

我正在尝试找到一种遍历所有元素(大陆 1、国家/地区 1、城市 1)的方法,该方法不涉及多个循环,也不会破坏 LINQ 语句的嵌套结构。

这里有与此类似的问题,但我还没有找到一个我足够理解的问题来与我的代码集成。

多谢!

C# 循环访问嵌套的 LINQ select 语句

"位置

"列表中包含的匿名类型具有包含City1成员的.Cities属性:

Cities = from Cities in Countries.Elements(ns + "City")
    select new
    {
        City1 = (string) Cities.Element(ns + "London")
    }

试试这个:

var Locations =
    from Continents in xml.Descendants(ns + "Continent")
    from Countries in Continents.Elements(ns + "Country")
    from Cities in Countries.Elements(ns + "City")
    select new
    {
        Continent1 = (string) Continents.Element(ns + "Europe"),
        Country1 = (string) Countries.Element(ns + "United Kingdom"),
        City1 = (string) Cities.Element(ns + "London")
    };