【问题标题】:LINQ to xml: result based on attributesLINQ to xml:基于属性的结果
【发布时间】:2016-08-31 10:15:12
【问题描述】:

我有一个类似的 XML

<?xml version="1.0" encoding="UTF-8"?>
<schedule>

<Aircraft mdy="Thursday, September 1, 2016" Destination="London"  
  Source="Greece" ID="B747">
 <FROM>GRE</FROM>
 <TO>LON</TO>
 <Miles>3000</Miles>
 </Aircraft>

 <Aircraft mdy="Thursday, September 1, 2016" Destination="New York"  
  Source="Greece" ID="B747">
  <FROM>GRE</FROM>
  <TO>IT</TO>
  <Miles>20000</Miles>
  </Aircraft>

 <Aircraft mdy="Thursday, September 1, 2016" Destination="Mykonos"  
  Source="Athens" ID="A320">
  <FROM>ATH</FROM>
  <TO>MYK</TO>
  <Miles>300</Miles>
  </Aircraft>

  </schedule>

请帮助我完成以下查询。我的 LINQ 经验有限,处于新手水平。

var p = from a in reservation.Descendants("Aircrafts")
        where a.Attribute("ID").Value.Equals("B747")
        ...
        change array contents

我想根据 ID 属性动态改变下面数组的内容。例如如果 ID=="B747" 座位[100],否则如果 ID=="A320" 座位[200] 等。

static int p= 100;
public static bool[] seats;
seats = new bool[p];

提前致谢

【问题讨论】:

    标签: c# arrays xml linq


    【解决方案1】:

    要设置p,您可以这样做:

    var p = (from element in XDocument.Load("data.xml").Descendants("Aircraft")
             let type = element.Attribute("ID").Value
             where type.Equals("B747")
             select type == "B747" ? 100 :
                    type == "A320" ? 200 : 0).FirstOrDefault();
    

    你也可以这样做:(然后一开始就加载,每次都使用)

    var seatsForModel = (from element in reservation.Descendants("Aircraft")
                         let type = element.Attribute("ID").Value
                         select new
                         {
                            AircraftModel = type,
                            Seats = type == "B747" ? 100 :
                                    type == "A320" ? 200 : 0
                         }).Distinct().ToDictionary(key => key.AircraftModel, value => value.Seats);
    
    // Output: [B747, 100],
    //         [A320, 200]
    
    bool[] seatsTaken = new bool[seatsForModel["B747"]];
    

    我创建一个飞机模型的Dictionary&lt;string,int&gt; 到它的座位数,然后你可以用它来创建你的bool[]

    虽然...我的一个更好的设计是在模型和座位数之间有一个映射,然后将它连接到 xml 中的项目


    至于在另一个函数中无法访问p,将linq包装在一个函数中并从另一个函数中调用它:

    public int GetNumberOfSeats(string aircraftModel)
    {
        var p = (from element in XDocument.Load("data.xml").Descendants("Aircraft")
                 let type = element.Attribute("ID").Value
                 where type.Equals(aircraftModel)
                 select type == "B747" ? 100 :
                        type == "A320" ? 200 : 0).FirstOrDefault();
        return p;
    }
    
    public void assignFirstClass()
    {
        var p = GetNumberOfSeats("B747");
        bool[] seats = new bool[p];
    
        //Rest of code
    }
    

    【讨论】:

    • 您好 Gilad,非常感谢您的帮助,目前我在移动设备上,我将在我的电脑上尝试您的解决方案。干杯,伙计!
    • 早上好,您的解决方案看起来很棒,但从函数中看不到数组。
    • 它说名称'seats'在当前上下文中不存在'。第二种方法呢?我还没有尝试过字典,因为我不明白如何使用它,我还在用谷歌搜索它。每次使用它是什么意思?
    • @user3136729 - 请参阅有关如何将 p 用于其他功能的更新。我建议更多地阅读静态以及何时使用它。关于字典,你可以有一个本地变量来存储它,然后每次你需要座位时,你都可以解决它,而不是再次访问文件并从中读取
    • @user3136729 - 关于p 的错误,我建议在使用 linq 和读取文件之前更多地学习过程和 oop 编程等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多