03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

770 ❘ Appendix J LINQ<br />

(continued)<br />

Function<br />

Nodes<br />

NodesAfterSelf<br />

NodesBeforeSelf<br />

Returns<br />

IEnumerable containing the nodes that are immediate children<br />

of the element. These include all nodes such as XElement<br />

and XText.<br />

IEnumerable containing the sibling nodes of the element that<br />

come after this element.<br />

IEnumerable containing the sibling nodes of the element that<br />

come before this element.<br />

LINQ to ADO.NET<br />

LINQ to ADO.NET provides tools that enable you to apply LINQ-style queries to objects used by<br />

ADO.NET to store and interact with relational data. LINQ to ADO.NET includes three components:<br />

LINQ to SQL, LINQ to Entities, and LINQ to DataSet.<br />

Building and managing SQL Server databases and the Entity Framework are topics too large to<br />

cover in this book, so LINQ to SQL and LINQ to Entities are not described in more detail here.<br />

For more information, consult the online help or Microsoft’s website.<br />

LINQ to DataSet lets a program use LINQ-style queries to select data from DataSet objects.<br />

For example, suppose the testScoresDataSet object contains tables named Students and<br />

TestScores. Then the following code gets references to the DataTable objects that represent<br />

the tables.<br />

DataTable studentsTable = testScoresDataSet.Tables["Students"];<br />

DataTable scoresTable = testScoresDataSet.Tables["TestScores"];<br />

You can then use LINQ to query the DataTable table objects. For example, the following code selects<br />

the names of students with LastName before "F" alphabetically.<br />

var namesBeforeFQuery =<br />

from student in studentsTable.AsEnumerable()<br />

where (student.Field("LastName").CompareTo("F") < 0)<br />

orderby student.Field("LastName")<br />

select new<br />

{<br />

FirstName = student.Field("FirstName"),<br />

LastName = student.Field("LastName")<br />

};<br />

namesBeforeDDataGrid.DataSource = namesBeforeFQuery.ToList();<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!