Net Framework 3 5
This course includes
? LINQ Overview
? Access array? Access set? Query projection
? Use a Lambda expression? Query operator
Agenda
? LINQ overview? Access array? Access set? Query projection
? Use a Lambda expression? Query operator
Data access problems
Data! = Object
Data access status
Sqlconnection c = new sqlconnection (...);
Queries in quotes
C. Open ();
Sqlcommand cmd = new sqlcommand (
@ "Selectc. Name, C. Phone
Arguments
From MERs C
Loosely bound
Where c City = @ P0"
);
Cmd. Parameters. addwithvalue ("@ po", "London"); datareader DR = C. Execute (CMD );
Results loosely
While (dr. Read ()){
Typed
String name = dr. getstring (0); string phone = dr. getstring (1)
Datetimedate = dr. getdatetime (2)} Dr. Close ();
Compilercannot help
Catch mistakes
Data Access Method
Classes describe
Public Class Customer
Data
{
Public int customerid;
Encapsulated
Public string name;
Business
Public String city;
Validation
Public void validate (){
Query is natural
// Todo: add business Validation
Part of
}
Language
}
Gridview1.datasource = from customer in db. MERs
Wherecustomer. City = "London"
Select customer;
The Compiler
Provides
Gridview1 databind ();
Intelliisense and
Type-checking
LINQ Project
C #
VB
Others...
. Net Language Integrated Query (LINQ)
LINQ enabled data sources
LINQ enabled ADO. net
LINQ
LINQ
LINQ
LINQ
LINQ
To objects
To Datasets
To SQL
To entities
To XML
<Book>
<Title/>
<Author/> <price/>
</Book>
Objects
Relational
XML
Agenda
? LINQ Overview
? Access Array
? Access set? Query projection
? Use a Lambda expression? Query operator
Query Arrays
Array implements
Ienumerable <t>
String [] cities = {"Auckland", "Oslo", "sysydney ",
"Seattle", "Paris", "Los Angeles "};
Ienumerable <string> places = from city in cities
Wherecity. length> 5
Orderby city descending select city;
Gridview1.datasource = places; gridview1.databind ();
LINQ Query
Expression Using
Query Operators
Ienumerable <string>
Sequence result can be
Used w/databinding
Bind to page
Agenda
? LINQ overview? Access Array
? Access set
? Query projection
? Use a Lambda expression? Query operator
Custom City class
Public class city {
Public string name;
Public String country;
Public int distancefromseattle;
}
List <city> locations = getlocations ();
Query the city collection
Collection implements
Ienumerable <t>
List <city> locations = getlocations ();
Ienumerable <city> places = from city in locations
Wherecity. distancefromseattle> 1000 orderby city. country, city. Name selectcity;
Gridview1.datasource = places; gridview1.databind ();
Ienumerable <city> return sequencedetermined by select statement
Bind to page
<Asp: gridview id = "gridview1" autogeneratecolumns = "false" runat = "server">
<Columns>
<Asp: boundfieldheadertext = "country" datafield = "country"/> <asp: boundfieldheadertext = "city" datafield = "name"/> <asp: boundfield headertext = "Dist" datafield = "distancefromseattle"/>
</Columns>
</ASP: gridview>
Page binding result
Agenda
? LINQ overview? Access array? Access set
? Query projection
? Use a Lambda expression? Query operator
Query projection (select)
? Do not return all data columns/attributes? Modify or convert the returned data
? Number of queries supported by the compiler for "anonymous type"
Data column/attribute
? Generate anonymous type ('a)
Use anonymous type
List <city> cities = cityutilityhelper. getcities ();
VaR places = from city in cities
Where city. distancefromseattle> 1000 select new {
City = city. Name,
Country = city. Country,
Distanceinkm = city. distancefromseattle * 1.61
};
Gridview1.datasource = places; gridview1.databind ();
Anonymous type used to custom shape dataresults and apply miles-> kilometer Conversion
Anonymous Type Binding
<Asp: gridview id = "gridview1" autogeneratecolumns = "false" runat = "server">
<Columns>
<Asp: boundfieldheadertext = "country" datafield = "country"/> <asp: boundfieldheadertext = "city" datafield = "city"/> <asp: boundfieldheadertext = "dist (km) "datafield =" distanceinkm "/>
</Columns>
</ASP: gridview>
Anonymous binding result
Agenda
? LINQ overview? Access array? Access set? Query projection
? Use a Lambda expression? Query operator
Use a Lambda expression
? The query syntax is a convenient abbreviation of declarative code.
You can write it manually:
? Ienumerable expr = names
. Where (S => S. Length = 5)
. Orderby (S => S)
. Select (S => S. toupper ());
Delegate declaration of lambda expressions
? The Lambda expression is the natural evolutionary result of the C #2.0 anonymous method.
Func filter = delegate (string s ){
Return S. Length = 5;
};
Func extract = delegate (string s ){
Returns;
};
Func project = delegate (string s ){
Returns. toupper ();
};
Ienumerable expr = names. Where (filter)
. Orderby (extract)
. Select (project );
Expression Tree
? The expression tree is the data representation in the effective memory of the lambda expression.
It makes the structure of the expression transparent and explicit. ? Assign a Lambda expression to a variable of expression type
, Field or parameter, the compiler will issue the Expression Tree.
Binaryexpression body = (binaryexpression) filter. Body; parameterexpression left = (parameterexpression) body. Left; constantexpression right = (constantexpression) body. Right;
Console. writeline ("{0} {1} {2}", left. Name, body. nodetype, right. value );
Definition of the query operator where
? Public static class SEQUENCE {
Publicstatic ienumerable where (this ienumerable source, func predicate ){
Foreach (T item in source)
If (predicate (item ))
Yield return item;
}
}
Call
? To call the extension method in a common way:
? Ienumerable <string> query =
Enumerable. Where (names, S => S. length <6 );
? C # language allows us to use the following method to call extensions
Method:
? Ienumerable <string> query = names. Where (S =>
S. length <6 );
Agenda
? LINQ overview? Access array? Access set? Query projection
? Use a Lambda expression
? Query operator
Standard query operator
? Sorting and grouping
-Orderby & groupby
? Aggregation
-Count-sum
-Average-max
-Min
? Projection
Select & selectworkflow
Query syntax
? The existing foreach Statement of C # uses the. NET Framework
The ienumerable/ienumerator method provides declarative syntax for iteration. The foreach statement is completely optional, but it has been proved to be a very convenient and commonly used language mechanism.
? The query syntax uses declarative syntax for the following most common query operations:
It simplifies the query expression: Where, select, selectmany, groupby, orderby, thenby, orderbydescending, and thenbydescending.