猿问

LINQ to XML 选择类似于 SQL IN 子句的元素

我有一些包含付款信息的 XML,其中包括付款所应用的发票的详细信息。例如,我想使用 LINQ to XML 仅收集适用于某些发票 (111,222,333) 的付款。如果是 SQL,我可以使用 IN (111,222,333) 关键字,但我不确定如何在 LINQ 中执行相同的操作。


<Payment>

    <PaymentId>1</PaymentId>

    <Total>50</Total>

    <Invoice>

        <Id>111</Id>

        <Amount>20</Amount>

    </Invoice>

    <Invoice>

        <Id>555</Id>

        <Amount>30</Amount>

    </Invoice>

</Payment>

<Payment>

    <PaymentId>2</PaymentId>

    <Total>20</Total>

    <Invoice>

        <Id>222</Id>

        <Amount>20</Amount>

    </Invoice>

</Payment>

<Payment>

    <PaymentId>3</PaymentId>

    <Total>80</Total>

    <Invoice>

        <Id>888</Id>

        <Amount>80</Amount>

    </Invoice>

</Payment>

LINQ


var result = xml.Select(x => x.Element("Payment"))

               Where(x => x.Element("Id").Value.Contains("111","222","333"))

在此示例中,我想选择“PaymentId 1”和“PaymentId 2”,因为它们适用于 ID 匹配 111、222 或 333 的发票。


不负相思意
浏览 100回答 1
1回答

肥皂起泡泡

您可以对这些知名 ID 的列表进行匹配/相交。List<string> ids = new List<string> { "111", "222", "333" };var result = xml&nbsp; &nbsp; .Elements("Payment")&nbsp; &nbsp; .Where(p => {&nbsp; &nbsp; &nbsp; &nbsp; var invoiceIds = p.Elements("Invoice").Elements("Id").Select(o => o.Value);&nbsp; &nbsp; &nbsp; &nbsp; return ids.Intersect(invoiceIds).Any();&nbsp; &nbsp; });完整代码和NetFiddle。const string XML = @"&nbsp; &nbsp; <root>&nbsp; &nbsp; &nbsp; &nbsp; <Payment>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <PaymentId>1</PaymentId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Total>50</Total>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Invoice>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Id>555</Id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Amount>30</Amount>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Invoice>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Invoice>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Id>111</Id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Amount>30</Amount>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Invoice>&nbsp; &nbsp; &nbsp; &nbsp; </Payment>&nbsp; &nbsp; &nbsp; &nbsp; <Payment>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <PaymentId>2</PaymentId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Total>20</Total>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Invoice>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Id>222</Id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Amount>20</Amount>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Invoice>&nbsp; &nbsp; &nbsp; &nbsp; </Payment>&nbsp; &nbsp; &nbsp; &nbsp; <Payment>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <PaymentId>3</PaymentId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Total>80</Total>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Invoice>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Id>888</Id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Amount>80</Amount>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Invoice>&nbsp; &nbsp; &nbsp; &nbsp; </Payment>&nbsp; &nbsp; </root>&nbsp; &nbsp; ";XElement xml = XElement.Parse(XML);List<string> ids = new List<string> { "111", "222", "333" };var result = xml&nbsp; &nbsp; .Elements("Payment")&nbsp; &nbsp; .Where(p => {&nbsp; &nbsp; &nbsp; &nbsp; var invoiceIds = p.Elements("Invoice").Elements("Id").Select(o => o.Value);&nbsp; &nbsp; &nbsp; &nbsp; return ids.Intersect(invoiceIds).Any();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; );foreach (var item in result){&nbsp; &nbsp; Console.WriteLine("PaymentId: {0}", (string)item.Element("PaymentId"));}
随时随地看视频慕课网APP
我要回答