Items
where itms.CategoryID == 1
select new {Itemname = itms.Name, itms.Ingredients}
into newTable orderby newTable.Itemname
select newTable;
This query has a new object called newTable, which will get created based on the
Select statement, which selects Name and Ingredients of the items. We can also
order the result-set using one of the column values.
Constructing XML
We have used projections for fetching data from the database tables in different
ways. Queries should be flexible enough to get the data in whichever format we like.
Getting data as XML is another important requirement in applications nowadays.
Using LINQ to SQL, we can easily build XML elements. The following code shows
how to get data from the Items table into an XML file:
var IcecreamsasXML =
new XElement("Icecreams",
from itms in db.Items
where itms.CategoryID == 1
select new XElement("Icecream",
new XElement("Name", itms.Name),
new XElement("ServingSize", itms.ServingSize),
new XElement("Protein", itms.Protein),
new XElement("TotalCarbohydrates", itms.
Pages:
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182