Realtime Linq Mapper

  • View
    402

  • Download
    3

Embed Size (px)

Text of Realtime Linq Mapper

Realtime Development Framework applied to LINQ(.NET Language-Integrated Query) LINQ DLINQ ORM (Object Relational Mapping) Implement Visual Studio 2008 (mapping) Relational Database OO Type Database OO

nw 12/13/2007

Realtime Development Framework applied to LINQ (.NET Language-Integrated Query) LINQ DLINQ ORM (Object-Relational Mapping) Implement Visual Studio 2008 (mapping) Relational Database OO Type Database OO

Database NVarChar

Mapper String

LINQ

LINQ to Objects LINQ Object LINQ to Datasets, LINQ to SQL, LINQ to Entities Relational Database LINQ to XML XML Document Realtime Development Framework LINQ to SQL Class Object Database Table, View Function, Method Select, Insert, Delete, Update Database Virtual Object Database Library DLL File LINQ to SQL SQL Statement string Execute database Return Result Set DataTable DataReader SQL IntelliSense Execute Database Throw Database Exception LINQ Query Compile time Intellisence

Mapping Table LINQ to SQL Database OO

ORM DataContext Database Database DataContext ADO.NET Connection ( SqlConnection Connection String app.config) DataContext 1. Database Connection 2. LINQ Query Syntax SQL Statement Execute 3. Object Query Database Database Modeling (.dbml) Mapping Table, Stored Procedure . LINQ To SQL 1. LINQ Query Syntax 2. LINQ Query Syntax SQL Statement 3. SQL Statement Execute Execute Database Datatype Database Row 4. ObjectType Mapping Table 5. Property Entity Object Method SubmitChanges() Class DataContext 6. Entity Property SQL Statement DML Stored Procedure SQL Server

Implicitly typed local variables keyword var datatype datatype implicitly type local variable Object Type var Primitive Type ( pointer memory) cast Pointer Type variant (var) strong typed .Net var foo = 0.8; Compiler variant object initialization Anonymous types C#3.0 object type object object initialization var mySudent = new Student{ name = "Pravit", Age = 45, address = "109/20" }; class Student { public string name; public string address; int age; public int Age { get { return age; } set { age = value; } } } object myStudent type var implicit type local variables compile myStudent type Student Anonymous types object

var

mySudent

=

new { name

= "Pravit",

Age

= 45, address

=

"109/29" };

constructor type object myStudent type error compiler type Anonymous types object type Syntax object initializes type Implicitly typed local avariables Complier type complie (late binding)

Implicitly typed arrays C#3.0 array type, size and dimension C# 2.0 array int[] arr = new int[] { 1, 2, 3, 4 }; C#3.0 var arr = new[] { 1, 2, 3, 4 }; type compile arr data type int Implicitly typed arrays array type syntax array type implicitly type local variables Compiler type compile

Lambda Expression C# 3.0 anonymous method C# 2.0 Lambda Expression delegate, DLINQ expression tree c C# x => x > 10 Boolean x 10 LINQ DLINQ (DLINQ LINQ C# 3.0) compile Lambda Expression DLINQ

var myQ = from myTable in orders, c in customers where ( myTable.ShipCity == "London") && ( myTable.CustomerID == c.CustomerID) select new { myTable.OrderDate, c.CompanyName, c.ContactTitle, c.ContactName }; DLINQ object myTable object collection order c customers DLINQ var myQ = orders .Where(myTable => myTable.ShipCity == "London") .SelectMany(myTable => customers .Where(c => myTable.CustomerID == c.CustomerID) .Select(c => new { myTable.OrderDate, c.CompanyName, c.ContactTitle, c.ContactName })); Lambda Expression 2 myTable => myTable.ShipCity == "London" c => myTable.CustomerID == c.CustomerID Lambda Expression Parameter => Expression Parameter myTable expression myTable.ShipCity == "London" C# 3.0 generic delegate delegate delegate delegate delegate T T T T Func(A Func(A0 param0, A1 param1, A2 param2, A3 param3)

Lambda Expression delegate Lampda Expression Func isEven = i => (i&1) == 0 Lambda Expression C#3.0 anonymous method C#2.0 delegate LINQ

Query Expression Query syntax from select or group from select or group where, orderby, join, let keyword into join group Query expression List var scores = new[] { 90, 71, 82, 93, 75, 82 }; List cities = new List(); List countries = new List(); public class { public public public } Country string Name { get; set; } int Area { get; set; } List City { get; set; }

public class City { public int Population { get; set; } } Query variable

80 IEnumerable scoreQuery = from score in scores where score > 80 orderby score descending select score; output = 93 90 82 82

int highestScore = (from score in scores select score) .Max(); IEnumerable scoreQuery = from score in scores select score; int highScore = scoreQuery.Max(); output = 93

City Population > 1000

//Query syntax IEnumerable queryMajorCities = from city in cities where city.Population > 1000 select city; // Method-based syntax IEnumerable queryMajorCities2 1000); = cities.Where(c => c.Population >

City country Population > 1000 IEnumerable largeCityList = from country in countries from city in country.City where city.Population > 1000 select city; Syntax query expression Explicitly and Implicit Typing of Query Variables // Use of var is optional here and in all queries. // queryCities is an IEnumerable just as // when it is explicitly typed. var queryCities = from city in cities where city.Population > 100 select city; Ending a Query Expression

Select with group byvar queryCountryGroups = from country in countries group country by country.Name[0];

Select with order byIEnumerable sortedQuery = from country in countries orderby country.Area select country; var queryNameAndPop = from country in countries select new { Name = country.Name, Pop = country.Population };

Continuations with "into"var percentileQuery = from country in countries let percentile = (int)country.Area / 2 group country by percentile into countryGroup where countryGroup.Key >= 20 orderby countryGroup.Key select countryGroup;

where ClauseIEnumerable queryCityPop = from city in cities where city.Population < 200000 && city.Population > 100000 select city;

order by ClauseIEnumerable querySortedCountries = from country in countries orderby country.Area > 500000, country.Area descending select country;

Join Clausevar categoryQuery = from cat in categories join prod in products on cat equals prod.Category select new { Category = cat, Name = prod.Name };

Let ClauseIEnumerable queryScr = from scr in scores let scMin = scr-10 select scMin; output = 80, 61, 72, 83, 65, 72

Subqueries in a Query Expressionvar queryGroupMax = from student in students group student by student.GradeLevel into studentGroup select new { Level = studentGroup.Key, HighestScore = (from student2 in studentGroup select student2.Scores.Average()) .Max() };

Join inner join, group join, left outer join class JoinDemonstration { #region Data class Product { public string Name { get; set; } public string CategoryID { get; set; } } class Category { public string Name { get; set; } public string ID { get; set; } } // Specify the first data source. List categories = new List() { new Category(){Name="Beverages", ID="001"}, new Category(){ Name="Condiments", ID="002"}, new Category(){ Name="Vegetables", ID="003"}, new Category() { Name="Grains", ID="004"}, new Category() { Name="Fruit", ID="005"} }; // Specify the second data source. List products = new List() { new Product{Name="Cola", CategoryID="001"}, new Product{Name="Tea", CategoryID="001"}, new Product{Name="Mustard", CategoryID="002"}, new Product{Name="Pickles", CategoryID="002"}, new Product{Name="Carrots", CategoryID="003"}, new Product{Name="Bok Choy", CategoryID="003"}, new Product{Name="Peaches", CategoryID="005"}, new Product{Name="Melons", CategoryID="005"}, new Product{Name="CityCar", CategoryID="008"}, new Product{Name="Toyota", CategoryID="008"}, }; #endregion static void Main(string[] args) { JoinDemonstration app = new JoinDemonstration(); app.InnerJoin(); app.GroupJoin(); app.GroupInnerJoin(); app.GroupJoin3(); app.LeftOuterJoin(); app.LeftOuterJoin2(); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } void InnerJoin() {

// Create the query that selects // a property from each element. var innerJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID select new { CategoryID = category.ID,CategoryName = category.Name, Product = prod.Name }; Console.WriteLine("InnerJoin:"); // Execute the query. Access results // with a simple foreach statement. foreach (var item in innerJoinQuery) { Console.WriteLine("{0} {1} {2}", item.Product,item.CategoryName, item.CategoryID); } Console.WriteLine("InnerJoin: {0} items in 1 group.", innerJoinQuery.Count()); Console.WriteLine(System.Environment.NewLine); } void GroupJoin() { // This is a demonstration query to show the output // of a "raw" group join. A more typical group join // is shown in the GroupInnerJoin method. var groupJoinQue