Now, if I would like to take
records from second to fifth in order, leaving the other records, we would have to
make use of the Skip() and Take() operators. The following code, shows how we
can apply these operators in the above query.
XElement NewIcecreamList = new XElement("IcecreamsList",
(from c in Icecreams.Elements("Icecream")
where (c.Element("Price").Value == "10")
orderby c.Element("Flavour").Value
select new XElement("Icecream",
c.Element("Flavour").Value.ToUpper().Skip(1).Take(4))));
The query operators are the methods that can be operated on any objects that
implement IEnumerable
class. We will see how we can create an object, make it
IEnumerable, and use query operators to query the XML.
First, we'll create the class and include variables corresponding to the elements in the
XML. We'll create a constructor to initialize all the variables of the class.
class NewIcecreamList
{
public string flavor;
public string servingSize;
public double price;
public string nutrition;
public IcecreamList(string flv, string srvS,
double prc, string nut)
{
flavor = flv;
servingSize = srvS;
price = prc;
nutrition = nut;
}
};
LINQ to XML
[ 62 ]
After creating the class, we construct a query using the above class.
Pages:
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113