Foreach traverses all data in linq

Source: Internet
Author: User
Tags datetime

Foreach traverses all data in linq
Data that can be queried by linq

Object. For example, set, array, and string.
Link. It can be divided into linq to dataset and linq to SQL. The former is used for dataset queries, and the latter is used for SQL server database tutorial queries (I believe other databases will be supported in the future ).
Xml. Xml files and xml objects.
Advantages of linq

No matter what data is queried, it is a unified structure.
More powerful than other query methods.
You can perform a syntax check during compilation. Unlike an SQL string, you can only find any syntax error at runtime.
You can perform type check during compilation.
Intelliisense is supported during development.
Easy debugging.
Linq environment

To be used in. net framework 3.5, linq supports the following languages: c #3.0 and vb9. Therefore, you cannot use linq in windows 2000 because. net framework 2000 cannot be installed in windows 3.5.

Using system;
Using system. collections. generic;
Using system. linq;
Using system. text;

Public class mainclass {
Public static void main (){
List <customer> MERS = getcustomerlist ();
Var wacustomers =
From c in MERS
Where c. region = "r1"
Select c;
Foreach (var customer in wacustomers ){
Console. writeline ("customer {0 }:{ 1}", customer. Mermerid, customer. companyname );
Foreach (var order in customer. orders ){
Console. writeline ("order {0 }:{ 1}", order. id, order. orderdate );
            }
        }
    }
Static list <customer> getcustomerlist (){
List <product> emptree = new list <product> ();
Emptree. add (new product {productname = "a", category = "o", unitprice = 12, unitsinstock = 5, total = 36, orderdate = new datetime (2005, 1, 1), id = 1 });
Emptree. add (new product {productname = "B", category = "o", unitprice = 2, unitsinstock = 4, total = 35, orderdate = new datetime (2005, 1, 1), id = 1 });
Emptree. add (new product {productname = "c", category = "o", unitprice = 112, unitsinstock = 3, total = 34, orderdate = new datetime (2005, 1, 1), id = 1 });
Emptree. add (new product {productname = "d", category = "o", unitprice = 112, unitsinstock = 0, total = 33, orderdate = new datetime (2005, 1, 1), id = 1 });
Emptree. add (new product {productname = "e", category = "o", unitprice = 1112, unitsinstock = 2, total = 32, orderdate = new datetime (2005, 1, 1), id = 1 });
Emptree. add (new product {productname = "f", category = "o", unitprice = 11112, unitsinstock = 0, total = 31, orderdate = new datetime (2005, 1, 1), id = 1 });

List <customer> l = new list <customer> ();
L. add (new customer {companyname = "a", region = "r1", unitsinstock = 1, orders = emptree, customerid = 0 });
L. add (new customer {companyname = "B", region = "r2", unitsinstock = 2, orders = emptree, customerid = 1 });
L. add (new customer {companyname = "c", region = "r3", unitsinstock = 3, orders = emptree, customerid = 2 });
L. add (new customer {companyname = "d", region = "r4", unitsinstock = 4, orders = emptree, customerid = 3 });
L. add (new customer {companyname = "e", region = "r5", unitsinstock = 5, orders = emptree, customerid = 4 });
Return l;
    }
}

Class customer: icomparable <customer> {
Public string companyname {get; set ;}
Public string region {get; set ;}
Public list <product> orders {get; set ;}
Public int unitsinstock {get; set ;}
Public int customerid {get; set ;}

Public override string tostring (){
Return string. format ("id: {0}, name: {1}, region: {3}", this. customerid, this. companyname, this. region );
    }
Int icomparable <customer>. compareto (customer other ){
If (other = null)
Return 1;

If (this. customerid> other. customerid)
Return 1;

If (this. customerid <other. customerid)
Return-1;

Return 0;
    }
}
Class product: icomparable <product> {
Public string productname {get; set ;}
Public string category {get; set ;}
Public int unitprice {get; set ;}
Public int unitsinstock {get; set ;}
Public int total {get; set ;}
Public datetime orderdate {get; set ;}
Public int id {get; set ;}

Public override string tostring (){
Return string. format ("id: {0}, name: {1}, category: {3}", this. id, this. productname, this. category );
    }
Int icomparable <product>. compareto (product other ){
If (other = null)
Return 1;
If (this. id> other. id)
Return 1;

If (this. id <other. id)
Return-1;

Return 0;
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.