在对象中具有不同级别的C#Lambda

时间:2018-10-25 15:38:40

标签: c# lambda

我正在尝试对一个对象执行lambda,但是我在不同级别的对象中都有条件。

该对象序列化为xml只是为了向您显示:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSegment>
    <Segment>
    <Legs>
      <Leg>
        <FlightDesignator>
          <CarrierCode >G3</CarrierCode>
          <FlightNumber >1040</FlightNumber>
        </FlightDesignator>
      </Leg>
    </Legs>
    <PaxSegments >
      <PaxSegment>
        <LiftStatus>Boarded</LiftStatus>
      </PaxSegment>
      <PaxSegment>
        <LiftStatus>CheckedIn</LiftStatus>
      </PaxSegment>
    </PaxSegments>
    </Segment>  

    <Segment>
    <Legs>
      <Leg>
        <FlightDesignator>
          <CarrierCode >G3</CarrierCode>
          <FlightNumber >1016</FlightNumber>
        </FlightDesignator>
      </Leg>
    </Legs>
    <PaxSegments >
      <PaxSegment>
        <LiftStatus>Boarded</LiftStatus>
      </PaxSegment>
      <PaxSegment>
        <LiftStatus>CheckedIn</LiftStatus>
      </PaxSegment>
    </PaxSegments>
    </Segment>  
</ArrayOfSegment>

我要做的是使用"FlightNumber" == 1016"LiftStatus" == "Boarded"获取段,但是它们位于不同级别的xml中。

一些lambda专家可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

在尝试回答您的问题之前,我希望您确认这些类型与C#类层次结构中的类型非常匹配:

public class Segment
{
    public List<Leg> Legs { get; set; }
    public List<PaxSegment> PaxSegments { get; set; }
}
public class Leg
{
    public FlightDesignator FlightDesignator { get; set; }
}
public class FlightDesignator
{
    public string CarrierCode { get; set; }
    public string FlightNumber { get; set; }
}
public class PaxSegment
{
    public string LiftStatus { get; set; }
}

第二,我从您的XML推断出这是您正在使用的细分的列表:

var segments = new List<Segment>
{
    new Segment
    {
        Legs = new List<Leg>
        {
            new Leg {
                FlightDesignator = new FlightDesignator{
                    CarrierCode = "G3",
                    FlightNumber = "1040"
                }
            }
        },
        PaxSegments = new List<PaxSegment> {
            new PaxSegment { LiftStatus = "Boarded" },
            new PaxSegment { LiftStatus = "CheckedIn" }
        }
    },
    new Segment {
        Legs = new List<Leg>
        {
            new Leg{
                FlightDesignator=new FlightDesignator
                {
                    CarrierCode="G3",
                    FlightNumber="1016"
                }
            }
        },
        PaxSegments = new List<PaxSegment> {
            new PaxSegment { LiftStatus = "Boarded" },
            new PaxSegment { LiftStatus = "CheckedIn" }
        }
    }
};

如果是这样,您可以通过以下查询获得所需的Segment

var desiredSegments = segments.Where(s
    => s.Legs.Any(l => l.FlightDesignator.FlightNumber == "1016")
    && s.PaxSegments.Any(ps => ps.LiftStatus == "Boarded")
);

但是,您的问题尚不清楚,因此,如果您要查找对应的PaxSegment中的Segment,则可以跟进SelectMany以获取答案他们:

var desiredPaxSegments = segments
    .Where(s
        => s.Legs.Any(l => l.FlightDesignator.FlightNumber == "1016")
        && s.PaxSegments.Any(ps => ps.LiftStatus == "Boarded"))
    .SelectMany(s => s.PaxSegments);

答案 1 :(得分:0)

使用xml linq:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication75
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var status = doc.Descendants("Segment").Select(x => new
            {
                flightNumber = (string)x.Descendants("FlightNumber").FirstOrDefault(),
                carrierCode = (string)x.Descendants("CarrierCode").FirstOrDefault(),
                liftStatus = x.Descendants("LiftStatus").Select(y => (string)y).ToList()
            }).ToList();
        }

    }
}