1 / 15

Advanced Programming Chapter 9: Introduction to LINQ & the List Collection

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.

nstern
Download Presentation

Advanced Programming Chapter 9: Introduction to LINQ & the List Collection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced ProgrammingChapter 9: Introduction to LINQ &the List Collection Dr Shahriar Bijani Shahed University Spring 2018

  2. Reference • Chapter 9, Visual C# 2012 How to Program, Paul Deitel & Harvey Deitel, 5th Edition, Prentice Hall.

  3. 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

  4. 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.

  5. 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};

  6. 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.

  7. LINQ to Objects using an int array • Fig. 9.2

  8. 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.

  9. 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.

  10. 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.

  11. 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

  12. 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

  13. 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;

  14. Lists

More Related