LINQ to SQL additions and deletions

Source: Internet
Author: User

ORM "Object Relation Mapping"

Linq to SQL:

First, build the LINQ to SQL class:
Understanding Context Classes: Linq to SQL class name +context each table in the database can be accessed using a context class;


Classification Application *******************************************************
Note: 1 info is the actual referenced table name
2 You need to instantiate the context class first when performing a function.

★★★★★★★★★★★★★★★★★★★★, query class ★★★★★★★★★★★★★★★★★★★★★★★★★
Mydbdatacontext context= New Mydbcontext (),//instantiate the context class
1 queries All and displays
Mydbdatacontext context= new Mydbcontext ();//Instantiate the context class
List<info> list = Context.info.tolist (); Perform a query operation;
Datagridview1.datasouse = list;//display the queried data
2 single condition query--equivalent query
var mama = from P in context. Info wher E P.code = = "n002" SELECT p//query mode one
var mama = context.info where (p=>p.code== "n002");//query mode two lambda expression

Datagridview1.datasouce = Mama//Send to page display;
3 single-condition query----not equivalent query
var mama = from P in Context.info where p.code! = "N0 "Select P//Fayi
var mama = context.info where (P=>p.code!=n002);//Fah lambda expression
var mama = context.info WH ere (p = = datatime.now-p.birthday.values.year); Method of extracting time

Datagridview1.datasouce = Mama//Send to page display;
over 4 pieces of query:
//var query = from P in _context.info where p.sex = = true & amp;& p.birthday.value.year > 1985 Select P; French one
//var query = _context.info.where (p=>p.sex==true && p.birthday.value.year>1985); method two LANBDA expression
var query = _context.info.where (p = = P.sex = True). Where (p=>p.birthday.value.year>1985); Multi-conditional logic with a chained expression can be used to write
Datagridview1.datasource = query;
5 fuzzy query:
var mama = from P in Context.info where P.name Co Ntains ("Zhang") selsect p;//equivalent to like '% of% ' fuzzy query--information containing what characters
//var query = from P in _context.info where P.name.startswith ( "Zhang") select P; Like ' sheet% ' fuzzy query--information beginning with what character

//var query = from P in _context.info where P.name.endswith ("Zhang") select P;//like ' % ' Fuzzy query--what character ends with information
//var query = from P in _context.info where p.name.substring (total) = = "Zhang" select p;//like ' _ Sheet% ' mode Paste Query--The information of what character is a number

var query = _context.info.where (p = = P.name.startswith ("Zhang"));
6 Set operation:
var q1 = from P in _context.info where p.sex== true select P;
var q2 = _context.info.where (P = p. Birthday.Value.Year > 1985);

Intersection: var query = Q1. Intersect (Q2);
Set: var query = Q1. Union (Q2);
Difference set: var query = Q2. Except (Q1);
7 Statistical functions:

var query = _context.info.where (p = = p.birthday.value.year > 1985);
//this. Text = query. Count (). ToString (); Number of
//this. Text = query. Sum (p=>datetime.now.year-p.birthday.value.year). ToString (); Sum
//this. Text = query. Average (p = datetime.now.year-p.birthday.value.year). ToString ();//averaging
//this. Text = query. Max (p = datetime.now.year-p.birthday.value.year). ToString (); The maximum value of
this. Text = query. Min (p = datetime.now.year-p.birthday.value.year). ToString (); To find the minimum value

7 Conversion actions:
var mama = from P in Context.info where P.name contains "Zhang" select p;

list<info> list = Mama. ToList (); To convert the query results to a generic collection:
info[] List = Mama. ToArray (); Convert the results of the query into arrays;
Info data = Mama.tosingle (); Convert query results to a single object
Info data= mama.tofirst (); Convert query results to a single object

8 Paging the query results
How many pagesize per page, to find the first few pages PageNo
var query = _context.info.skip (2*3). Take (2); From the query results skip (jump) 2 rows per row 3 records to take the jump 2 rows;

Datagridview1.datasource = query;
9 Special Usage Records
1 Removing duplicate statements from a query: var mama = from P in Convert.info where P.name contains "Zhang" select p. distinct ();


★★★★★★★★★★★★★★★★★★★★ Add Class ★★★★★★★★★★★★★★★★★★★★★★★★★
Add action:
Mydbdatacontext context= New Mydbconte XT ();//Instantiate the context class
Info data = new info ();//The table you want to add is instantiated first;
data. Code = Txtcode.text; To initialize
data. Name = txtName.Text;
Data.sex =convert.toboolean (textsex.text);
Data.nation =txtnation.text;
Data. Birthday = Convert.todatetime (Textbirthday.text);

Context.info.insertOnSubmit (data);//Tell the context class to perform an insert operation when clicking Submit;

Context.submitchanges ();//context submission to database

★★★★★★★★★★★★★★★★★★★★ Delete class ★★★★★★★★★★★★★★★★★★★★★★★★★
Delete
//Find the object to be deleted by the context class
Mydbdatacontext C ontext= new Mydbcontext ();//Instantiate the context class
var sc = from mama in Context.info where Mama. Code Select Mama;
if (Sc.count () >0)
Info data = Sc.first ();
//Tell context
Context.info.delectOnsubmit (data);
//Context class Commit To the Database
Context.submitchangs ();

★★★★★★★★★★★★★★★★★★★★ Four, modify the class ★★★★★★★★★★★★★★★★★★★★★★★★★
Modify Operation
Mydbdatacontext context= new Mydbcontext ();//Instantiate the context class
Find the data you want to modify
var Tianjia = from mama in context.info where p.code = = Txtcode.text Select Mama
modifying data
Data. Name = txtName.Text;
Data. Sex = Convert.toboolean (Txtsex.text);
Data. Nation = Txtnation.text;
Data. Birthday = Convert.todatetime (Txtbirthday.text);
Context class commits to the database
Context.submitchanges ();

LINQ to SQL additions and deletions

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.