In this week's C # course, we learned some of the things we need to do with database or XML files in C # programming, which is LINQ, which makes a query language similar to the SQL language we use in our databases a part of the C # language. Convenient for us to search the database and other aspects of operations. Let's review it together.
(i) Create LINQ (Creating the Query):
1.From words (the FROM clause): Specifying range variables and data sources
from Customer in customers//Customer:range variable from the data source. // Customers:data source can is any collection that implements System.Collections.Generic. ienumerable<t>
2. Filter (Filtering): Need to use where and be called filter
where " Donna " // a filter is a Boolean expression
where customer. Lastname.startwith ("G")//can also use composite expressions to construct more complex queries
3. Map or select (Projection): The select sentence, which defines how the result is returned.
Select customer; // defines (or projects) the Results.the query returns the customer objects.
(ii) LINQ and C #:
Connection (joining):
I found some simple samples on MSDN that the teacher mentioned in class:
1.Cross Join:
Public voidLinq102 () {string[] Categories =New string[]{ "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" }; List<Product> products =getproductlist (); varQ = fromCinchcategories Join PinchProducts on c equals P.categorySelect New{Category =c, p.productname}; foreach(varVinchq) {Console.WriteLine (V.productname+": "+v.category); } }
2. Here is an example of using a group join:
Public voidLinq103 () {string[] Categories =New string[]{ "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" }; List<Product> products =getproductlist (); varQ = fromCinchcategories Join PinchProducts on c equals p.category into PSSelect New{Category = c, products =PS}; foreach(varVinchq) {Console.WriteLine (V.category+":"); foreach(varPinchv.products) {Console.WriteLine (" "+p.productname); } } }
3.Cross join with Group join:
Public voidLinq104 () {string[] Categories =New string[]{ "Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood" }; List<Product> products =getproductlist (); varQ = fromCinchcategories Join PinchProducts on c equals p.category into PS fromPinchPSSelect New{Category =c, p.productname}; foreach(varVinchq) {Console.WriteLine (V.productname+": "+v.category); } }
Careful reading of the code, combined with the main function given in sample, allows us to better understand some of the usage of the join statement and help us to better grasp the LINQ statement.
Liam's C # Learning Journey (vi): LINQ (language-integrated Query)