Code
Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/-->using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data.Common;
Using System.Collections.Generic;
Namespace Firstlinq
{
public partial class _default:system.web.ui.page
{
NorthwindDataContext CTX = new NorthwindDataContext ("Data source=zhangshuqiang;initial catalog=northwind; Persist Security info=true; User Id=sa; Password=sa ");
protected void Page_Load (object sender, EventArgs e)
{
LINQ1 ();
LINQ2 ();
Linq3 ();
}
<summary>
Change and delete
</summary>
private void Linq3 ()
{
Increase
T_user User = new T_user ();
User. UserName = "big Weather";
User. PWD = "123456";
Ctx.t_User.InsertOnSubmit (User);//The previous method is add ();
CTx. SubmitChanges ();
Change
Refer to this syntax string s = (from A in CTX. Customers Select a). Single (a = a.ID = = 2);
T_user userupdate = ctx.t_User.SingleOrDefault (T_user = t_user.id = 2);//single is no different from Singleordefault
Userupdate.username = "big weather 1";
CTx. SubmitChanges ();
By deleting
T_user Userdelete = (from UserInfo in ctx.t_user where userinfo.id = = 1 Select userinfo). FirstOrDefault ();
if (userdelete! = null)
{
Ctx.t_User.DeleteOnSubmit (Userdelete);
CTx. SubmitChanges ();
}
}
<summary>
Familiarity with LINQ to SQL syntax
</summary>
private void Linq2 ()
{
Customers customer = new Customers ();
Execute normal SQL statements and query customerid= "ANATR" records
Ienumerable<customers> Customers = ctx. Executequery<customers> ("select * from Customers where customerid= ' Anatr '");
Customer = Customers. First ();
Response.Write (Customer. CustomerID);
Querying a single record using LINQ
var cus = from C in CTX. Customers where C.customerid.equals ("Anatr") select C;
Customer = cus. First ();
Response.Write (Customer.companyname);
Query result set, syntax: From temporary table name in table collection by the name of the temporary table. Field name upgrade select temporary table name
Gdvcustomers.datasource = from Cust in CTX. Customers where Cust. CustomerID! = "Anatr" by Cust.companyname descending select Cust;
Gdvcustomers.databind ();
}
<summary>
Familiarize yourself with the LINQ syntax, where variables are defined like JS, without declaring types.
</summary>
private void Linq1 ()
{
var age = 29;
var username = "Greatverve";
var userlist = new[] {"A", "B", "C"};
foreach (var user in userlist)
{
Response.Write (user);
}
}
}
}
Http://www.cnblogs.com/greatverve/archive/2010/05/13/1734236.html
LINQ to SQL additions and deletions (reprint)