160 likes | 185 Views
Explore the capabilities of LINQ (Language Integrated Query) in C# for querying arrays and Lists, along with an introduction to common LINQ providers. Learn about LINQ as declarative programming and its usage with various data types including user-defined classes. Additionally, delve into creating new types in a LINQ query and understand the basics of collections in .NET.
E N D
Advanced ProgrammingChapter 9: Introduction to LINQ &the List Collection Dr Shahriar Bijani Shahed University Spring 2018
Reference • Chapter 9, Visual C# 2012 How to Program, Paul Deitel & Harvey Deitel, 5th Edition, Prentice Hall.
Introduction • A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data in the database. • SQL language: the international standard used to perform queries and to manipulate data. • This chapter introduces C#’s new LINQ (Language Integrated Query) capabilities. • LINQ allows you to write query expressions, (similar to SQL queries), that retrieve information from a variety of data sources, not just databases. • Filtering:We use LINQ to Objects in this chapter to queryarrays and Lists
LINQ Providers • LINQ queries may be used in many different contexts because of libraries known as providers. • LINQ provider: is a set of classes that implement LINQ operations and enable programs to interact with data sources to perform tasks such as: • sorting, • grouping • filtering elements • Many LINQ providers are available: • LINQ to Objects (this chapter) • LINQ to Entities: to query databases • LINQ to XML: to query XML documents.
var: Implicitly Typed Local Variables • We can declare a local variable using var and let the compiler infer the variable’s type based on the variable’sinitializer. varx=7; • the compiler infers that the variable x should be of type int vary=-123.45; • he compiler infers that y should be of type double int[] values={2, 9, 5, 0, 3, 7, 1, 4, 8, 5}; int[] values=new[] {2, 9, 5, 0, 3, 7, 1, 4, 8, 5}; varvalues=new[] {2, 9, 5, 0, 3, 7, 1, 4, 8, 5};
LINQ as declarative programming • in imperative programming: we specify the actualsteps to perform a task vs • declarative programmingwe say what to do, not how to do it • LINQ specifies the conditions that selected elements must satisfy.
LINQ to Objects using an int array • Fig. 9.2
Interface IEnumerable<T> • most LINQ queries return IEnumerable<T> • foreachiterates over any IEnumerable<T> object, • IEnumerable<T> is an interface. • Interfaces define and standardize the ways in which people and systems can interact with one another. • The interface specifies what operations a radio permits users to perform but does not specify howthe operations are implemented. • A class that implements an interface must define each member in the interface with a signature identical to the one in the interface definition.
Interface IEnumerable<T> • Arrays are IEnumerable<T> objects, • LINQ query returns an IEnumerable<T> object. • the foreach statement can iterate through the contents of arrays, collectionsand LINQ query results.
More on LINQ • LINQ is not limited to querying arrays of simple types such as ints. It can be used with most data types, including strings and user-defined classes. • It cannot be used when a query does not have a defined meaning for example, you cannot use orderbyon objects that are not comparable. • Comparable types in .NET are those that implement the IComparableinterface. • All built-in types, such as string, intand double implement IComparable.
Creating New Types in the select Clause of a LINQ Query Employee e; …. new { e.FirstName, Last = e.LastName} • creates a new object of an anonymous type (a type with no name), which the compiler generates for you based on the properties listed in the curly braces ({}). • In this case, the anonymous type consists of properties for the first and last names of the selected Employee. • The LastNameproperty is assigned to the property Last in the select clause. This shows how you can specify a new name for the selected property. If you don’t specify a new name, the propert’soriginal name is used.this is the case for FirstName
Creating New Types in the select Clause of a LINQ Query • the compiler automatically creates a new class having properties FirstNameand Last, and the values are copied from the Employee object
Introduction to Collections • The .NET Framework Class Library provides several classes, called collections, used to store groups of related objects • Lists • The collection class List<T> • namespace System.Collections.Generic List <int> list1; List<string> list2;