Lecture 07 by aaz102



                                                          •   Arrays Declaring and Creating Arrays
                                                          •   foreach Statement
            Visual Programming                            •   Passing Arrays and Array Elements to Methods
                                                          •   Passing Arrays by Value and by Reference
         C# Programming with Microsoft                    •   Multidimensional Arrays
                    .NET                                  •   Variable-Length Argument Lists
                                                          •   Using Command-Line Arguments

                                                          • Large amounts of data are often stored in a database

                                                          • A database management system (DBMS) provides mechanisms for storing,
                                                            organizing, retrieving and modifying data in the database.

                                                          • A language called SQL—pronounced “sequel”—is the international standard
                     Lecture 7                              used to perform queries (i.e., to request information that satisfies given
                                                            criteria) and to manipulate data. For years, programs accessing a relational
                                                            database passed SQL queries to the database management system, then
                                                            processed the results
         Intro to LINQ, List and Collections
                                                          • Here we will use LINQ (Language Integrated Query).
                                                          • LINQ allows you to write query expressions, similar to SQL queries, that
                                                            retrieve information from a wide variety of data sources, not just databases

LINQ (Language Integrated Query)                                                        LINQ …
LINQ allows you to write query expressions, similar to    LINQ Providers
SQL queries, that retrieve information from a wide
                                                          • Set of classes that implement LINQ operations
variety of data sources, not just databases.
                                                          • Enable programs to interact with data sources
LINQ Providers The syntax of LINQ is built into C#, but   • Helps in preforming tasks such as sorting,
LINQ queries may be used in many different contexts         grouping and filtering elements.
because of libraries known as providers.

                                                          LINQ to SQL and LINQ to XML, which allow you
                                                          to query databases and XML documents using


        LINQ example with int array                                              LINQ example with int array 2
// create an integer array                                                  // create an integer array
 int[] values = { 2, 9, 5, 0, 3, 7, 1, 4, 8, 5 };                            int[] values = { 2, 9, 5, 0, 3, 7, 1, 4, 8, 5 };

// LINQ query that obtains values greater than 4                            // LINQ query that sort elements from the array
from the array                                                              var desorderedlist = from value in values
var filtered = from value in values                                         orderby value descending
where value > 4                                                             select value;
select value;

                                                                             The var Keyword and Implicitly Typed Local Variables
                           Example 2
Employee[] employees = {                                                    • You can also declare a local variable and let the compiler infer
new Employee( "Jason", "Red", 5000M ),                                        the variable’s type based on the variable’s initializer.
new Employee( "Ashley", "Green", 7600M ),                                   • To do so, the var keyword is used in place of the variable’s
              var between4K6K =
new Employee( "Matthew", "Indigo", 3587.5M ),
              from e in employees                                             type when declaring the variable. Consider the declaration
new Employee( "James", "Indigo", 4700.77M ),&& e.MonthlySalary <= 6000M
              where e.MonthlySalary >= 4000M                                • var x = 7;
new Employee( "Luke", "Indigo", 6200M ),
              select e;
new Employee( "Jason", "Blue", 3200M ),                                     • Here, the compiler infers that the variable x should be of type
              // display employees making between 4000 and 6000 per month     int, because the compiler assumes that whole-number values,
new Employee( "Wendy", "Brown", 4236.4M ) }; // end init list
              Console.WriteLine( string.Format(
               "\nEmployees earning in the range {0:C}-{1:C} per month:",     like 7, are of type int. Similarly, in the declaration
               4000, 6000 ) );                                              • var y = -123.45;
               foreach ( var element in between4K6K )
                Console.WriteLine( element );                               • the compiler infers that y should be of type double, because
                                                                              the compiler assumes that floating-point number values, like -
                                                                              123.45, are of type double.

                                   List                                                              Solution
.NET Framework Class Library provides several                               The collection class List<T>
classes, called collections, used to store groups                           (from namespace System.Collections.Generic)
of related objects.                                                         The T is a placeholder
Why list is required? Whats wrong with arrays?                              Replace it with the type of elements that you
Arrays do not automatically change their size at                            want the List to hold. Similar to specifying type
execution time to accommodate additional                                    of array. e.g.
elements—you must do so manually by creating                                List< int > list1; // List of integers
a new array or by using the Array class’s Resize
method.                                                                     List< String > list1; // List of String


Methods and Properties          Querying a Generic Collection
                                        Using LINQ
                         Use LINQ to Objects to query Lists just as arrays.
                         // populate a List of strings
                         List< string > items = new List< string >();
                         items.Add( "aQua" ); // add "aQua" to the end of the List
                         items.Add( "RusT" ); // add "RusT" to the end of the List
                         items.Add( "yElLow" ); // add "yElLow" to the end of the List
                         items.Add( "rEd" ); // add "rEd" to the end of the List

                         // convert all strings to uppercase; select those starting with "R"
                         var startsWithR =
                         from item in items
                         let uppercaseString = item.ToUpper()
                         where uppercaseString.StartsWith( "R" )
                         orderby uppercaseString
                         select uppercaseString;

      Thank you!


To top