如何優化 Linq to Xml 查詢反對屬性? (How do I optimize a Linq to Xml query againist attributes?)


問題描述

如何優化 Linq to Xml 查詢反對屬性? (How do I optimize a Linq to Xml query againist attributes?)

Given the following Xml fragment:

<root>
  <sheetData>
    <row r="1" />
    <row r="2" />
    <row r="3" />
    <row r="4" />
    <row r="5" />
    <row r="6" />
    <row r="7" />
  </sheetData>
</root>

Which can be created with the following code:

XElement testElement = new XElement("root",
    new XElement("sheetData",
        new XElement("row",
            new XAttribute("r", 1)),
        new XElement("row",
            new XAttribute("r", 2)),
        new XElement("row",
            new XAttribute("r", 3)),
        new XElement("row",
            new XAttribute("r", 4)),
        new XElement("row",
            new XAttribute("r", 5)),
        new XElement("row",
            new XAttribute("r", 6)),
        new XElement("row",
            new XAttribute("r", 7))));

Is this the best way to find the row where the r attribute is 2?  This works, but I am repeating the Where clause in the  select statement, and I am wondering if there is a better way and more efficent method.

int rowNumber = 2;

XElement rowElement = testElement
    .Descendants("sheetData")
    .Where<XElement>(item => item.Descendants("row")
                                  .Where<XElement>(i => i.Attribute("r").Value == rowNumber.ToString())
                                  .FirstOrDefault<XElement>() != null)
    .Select<XElement, XElement>(item => item.Descendants("row")
                                  .Where<XElement>(i => i.Attribute("r").Value == rowNumber.ToString())
                                  .FirstOrDefault<XElement>())
    .FirstOrDefault<XElement>();

In general what is the best way to determine if Linq to Xml query optimized?

‑‑‑‑‑

參考解法

方法 1:

The best way is:

var row = testElement
   .XPathSelectElements("sheetData/row[@r='2']")
   .FirstOrDefault();

A pure LINQ query that doesn't repeat the Where call:

var row = testElement
    .Descendants("sheetData")
    .Descendants("row")
    .Where(x => x.Attribute("r").Value == "2")
    .FirstOrDefault();

方法 2:

//if sheetData appears multiple times
    XElement rowElement = testElement
        .Descendants("sheetData")
        .SelectMany(s=>s.Descendats("row))
        .Where(i=>i.Attribute("r").Value == rowNumber.ToString());
//if sheetData appears once
    XElement rowElement = testElement
        .Element("sheetData")
        .Descendants("row))
        .Where(i=>i.Attribute("r").Value == rowNumber.ToString());

方法 3:

Isn't the Select clause in your code redundant? Where returns an IEnumerable<XElement> already. I think

var row = testElements.Descendents("row").Where(e => (int)e.Attribute("r") == rowNumber).SingleOrDefault();

should do what you want

(by David BasarabRobert RossneyeglasiusLee)

參考文件

  1. How do I optimize a Linq to Xml query againist attributes? (CC BY‑SA 3.0/4.0)

#.net-3.5 #optimization #linq-to-xml #C#






相關問題

如何在 C# 中搜索 pdf 中的文本(執行匹配) (How to search text (Exect match) in pdf in C#)

String.IsNullOrEmpty() (String.IsNullOrEmpty())

是否有可以綁定的可為空的日期選擇器? (Is there a nullable datepicker that I can bind to?)

具有自動命名屬性的通用組合框 (Generic ComboBox with automatically named properties)

在數據庫未定義的外鍵關係上配置實體框架 (Configuring Entity Framework on a Database Undefined Foreign Key Relationships)

我可以在 4.0 應用程序中引用 .NET 3.5 .DLL 嗎? (Can I reference a .NET 3.5 .DLL in a 4.0 app?)

如何在 ASP.NET、VB.NET 中解決這個會話問題? (How to tackle this session problem in ASP.NET,VB.NET?)

在 ADO.NET 數據服務中添加對查找表的引用 (Adding a reference to a lookup table in ADO.NET Data Services)

如何優化 Linq to Xml 查詢反對屬性? (How do I optimize a Linq to Xml query againist attributes?)

VS2005 和 LINQ (VS2005 and LINQ)

時區信息錯誤? (TimeZoneInfo error?)

與標準 C++ 相比,C++/CLI(以前稱為“託管 C++”)有哪些優勢? (What are the advantages of C++/CLI (formerly "Managed C++") over standard C++?)







留言討論