LINQ to SQL

Source: Internet
Author: User

LINQ is a language-integrated query instead of a database, which is more concise and easier to use than a database, of course, this is his advantage. His focus is on the query expression.
LINQ Query Expressions:
It consists of three main parts:
1. Get the data source
2. Create a query
3. Execute Query
Anonymous method:
Before 2.0, the only way to declare a delegate is to use a named method. Lambda expressions Replace anonymous methods in version 3.0 and later
Lambda expression:
1. What about him? An anonymous function that contains expressions and statements that can be used to create delegates
2. Lambda expression "X=>x * x" reads "x goes to X times X"
Query syntax and method statements:
We follow the three elements, first you need to have a data source.
String[] devices= {"TV", "refrigerator", "Washing machine", "telephone", "Microwave oven"};
Then there is the query syntax,
1. Select:
var selectdevices = from P in Devices
Select P;
And then there's a method statement with a forerch.
foreach (String dev in selectdevices)
{
Console.WriteLine ("The letter is: {0}", dev);
}
Here are some of the more complex query operations I think it's easy to write query statements if you learn LINQ from a database.
2. Where:
String[] devices= {"A", "B", "C", "D", "E"};
var selectdevices = from device in Devices
where P.startswith ("B")
Select P;
foreach (String dev in selectdevices)
{
Console.WriteLine ("The letter is: {0}", dev);
}
Or our three elements, is the same as the database in the middle of the inclusion of a Where condition
3.
String[] devices= {"A", "B", "C", "D", "E"};
var selectdevices = from P in Devices
where P.startswith ("B")
Select P;
foreach (String dev in selectdevices)
{
Console.WriteLine ("The letter is: {0}", dev);
}
4. Connection Query
Linqdbdatacontext db = new Linqdbdatacontext (linqsystem.linqdbconnectionstring);
var result = from the user in db. UserInfo
Join UR in db. userrole on user.id equals Ur. Userid
Join role in DB. Role on Ur. Roleid equals role.id
Where User.ID < 200
Select New {id = user.id,//display ID, user name, email address and role
UserName = user. Username,
Email = user. Email,
RoleName = role. RoleName
};
Dl_list.datasource = result;
Dl_list.databind ();

5. Aggregate Query
Linqdbdatacontext db = new Linqdbdatacontext (linqsystem.linqdbconnectionstring);
var result = from P in db. Product
Select p;//calculates the highest and lowest price of the item
Decimal Maxprice = result. Max (p = p.price.value);
Decimal Minprice = result. Min (p = p.price.value); Show highest and lowest prices
Response.Write (String. Format ("Maximum price: {0}, lowest price: {1}", Maxprice,minprice));
6. Group Query
Linqdbdatacontext db = new Linqdbdatacontext (linqsystem.linqdbconnectionstring);
var result = from P in db. Product
Group p by P.categoryid into G
G.key
Select New
{
Get product and category names
ProductName =g.first (). Name,
Catagoryname = (from u in db. Category
where U.id==g.key
Select U
). First (). Name
}; Data binding
Dl_list.datasource = result;
Dl_list.databind ();

Stored procedures:
1, add
Create proc pr_insertcategory (
@Name varchar () =null,
@ParentID int=null,
@ShowOrder Int=null,
@Remark text=null)
as
INSERT into Category ([Name],parentid,showorder,remark) values
(@Name, @ ParentID, @ShowOrder, @Remark)
Go
Adds the execution method:
String name = TextBox2.Text;
int id = Int. Parse (TextBox3.Text);
Tb_abc TB = new TB_ABC ()
{
Sname = name,
area_id = ID
};
Dc.tb_ABc.InsertOnSubmit (TB);
DC. SubmitChanges ();
Bind ();
Save more compact
Dc.p_addcreate (name,id);
bind ();
2. Delete
Create proc Deletecategroy
@ID int=null
as
Delete from Category where [email protected]
Go
Execution method for deletion:
(to invoke the "rowdeleting" event of the table)
String id = gridview1.datakeys[e.rowindex]. Value.tostring ();
var restr = from A in DC.TB_ABC
where a.sid.tostring () = = ID
Select A;
Dc.tb_ABc.DeleteAllOnSubmit (RESTR) ;
DC. SubmitChanges ();
Bind ();
Warm tip
: if (IsPostBack)
{
return;
}

LINQ to SQL

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.