No property or field 'OrderID' exists in type 'XElement' (at index 0)

c# dynamic-linq linq linq-to-xml

Question

i am reading a xml file and querying by LINQ this below way

XDocument document = XDocument.Load(xmlFilePath);
var query = document.Descendants("orders").Select(c => c);
query = query.OrderBy(sortColumn + " " + OrderDirection);

query = query.Skip(lowerPageBoundary - 1 * rowsPerPage).Take(rowsPerPage);

DataTable table = query.ToList().ConvertToDataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
//adapter.Fill(table);
return table;

but getting error No property or field 'OrderID' exists in type 'XElement' (at index 0)

this is my sample xml which i am querying by LINQ

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Orders>
    <OrderID>10248</OrderID>
    <CustomerID>VINET</CustomerID>
    <EmployeeID>5</EmployeeID>
    <OrderDate>1996-07-04T00:00:00</OrderDate>
    <RequiredDate>1996-08-01T00:00:00</RequiredDate>
    <ShippedDate>1996-07-16T00:00:00</ShippedDate>
    <ShipVia>3</ShipVia>
    <Freight>32.3800</Freight>
    <ShipName>Vins et alcools Chevalier</ShipName>
    <ShipAddress>59 rue de l'Abbaye</ShipAddress>
    <ShipCity>Reims</ShipCity>
    <ShipPostalCode>51100</ShipPostalCode>
    <ShipCountry>France</ShipCountry>
  </Orders>
</Root>

in debug mode i expand the result view and found order id exist. here is screen shot. enter image description here

so tell me where i made the mistake in code. please guide. thanks

1
1
7/21/2015 7:19:04 PM

Popular Answer

What are sortColumn and OrderDirection? By the usage it looks like they are strings, but OrderBy doesn't take a string unless you're using Dynamic LINQ.

LINQ to XML doesn't give you properties for Elements. If you have an XElement e, you wouldn't be able to call e.OrderID. You must use the Element method like this e.Element("OrderID").Value. To plug that into the OrderBy method, you could do this:

query = query.OrderBy(e => e.Element("OrderID").Value);

As the comment points out, adding .Select(c => c) doesn't do anything. Also, you could chain the remaining methods like this:

var query = document.Descendants("orders")
                    .OrderBy(e => e.Element("OrderID").Value)
                    .Skip(lowerPageBoundary - 1 * rowsPerPage)
                    .Take(rowsPerPage);

With Dynamic LINQ, you'll still need to use the Element method. Dynamic Linq to Xml example suggests that it should be something like this, but I haven't tested it:

var query = document.Descendants("orders")
                    .OrderBy(String.Format("Element(\"{0}\").Value {1}", sortColumn, OrderDirection))
                    .Skip(lowerPageBoundary - 1 * rowsPerPage)
                    .Take(rowsPerPage);

It should be possible without using Dynamic LINQ since you have to use the Element method and pass in the name of the column you're sorting by anyway. You'd just need to branch and use OrderByDescending when OrderDirection is "descending."

1
5/23/2017 11:52:24 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow