Early Learning of a simple LINQ example

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data. LINQ. Mapping;
Using system. Data. LINQ;

Namespace SCSI
{
[Table (name = "MERs")] // map the customers table in the database to the customer class.
Public Class Customer
{
[Column] // column as the name suggests
Public String customerid {Get; set ;}
[Column]
Public String city {Get; set ;}

Public override string tostring ()
{
Return customerid + "\ t" + city;
}
}

Class Program
{
Static void main (string [] ARGs)
{
// Numquery ();
Objectquery ();
Console. Read ();
}

Static ienumerable <customer> createcustomers ()
{
Return new list <customer>
{
New customer {customerid = "alfki", city = "Berlin "},
New customer {customerid = "bonap", city = "Marseille "},
New customer {customerid = "consh", city = "London "},
New customer {customerid = "eastc", city = "London "},
New customer {customerid = "Frans", city = "Torino "},
New customer {customerid = "lonep", city = "Portland "},
New customer {customerid = "norts", city = "London "},
New customer {customerid = "thebi", city = "Portland "}
};
}

Static void objectquery ()
{
// Datacontext is the most basic and important thing of LINQ. It is the entry point of the LINQ to SQL statement. For details, please query the msdn
VaR DB = new datacontext (@ "Data Source = shni010 \ sqlexpress; initial catalog = northwind; Integrated Security = true ");

// Var Results = from C in createcustomers ()
// Where C. City = "London"
// Select C. customerid;
// The query statement of LINQ
VaR Results = from C in db. gettable <customer> ()
Where C. City = "London"
Select C;
DB. log = console. out;
Foreach (var c in results) // loop result set
{
Console. writeline ("{0} \ t {1}", C. customerid, C. City );
}
Console. Read ();
}

// The following method shows the query of arrays by LINQ.
Static void numquery ()
{
VAR numbers = new int };
VaR evennumbers = from P in numbers
Where (P % 2) = 0
Select P;
Console. writeline ("Result :");
Foreach (var val in evennumbers)
{
Console. writeline (VAL );
}
Console. Read ();
}
}
}

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.