c#→ about the table<tentity> generic class under System.Data.Linq

Source: Internet
Author: User

Table<tentity> represents a tabular record, which is a generic collection class whose elements are table entity objects. It provides a set of methods for adding and deleting elements, and you can save them to the database through DataContext.

Table or the previous table, add a LINQ to SQL class to the project. The focus is insertonsubmit, DeleteOnSubmit and other methods.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Linq_to_sql_table
{
<summary>
Table<tentity> class for manipulating a single table
</summary>
Class Program
{
static void Main (string[] args)
{
1.a.attach Additional Entities
Dataclasses1datacontext DC1 = new Dataclasses1datacontext ();
Tb_guestinfo guset = new Tb_guestinfo () {id=1, Name = "Debuglzq", age = +, Tel = "138****8888"};

Dc1.tb_GuestInfo.Attach (Guset);//Such Attach only attach entities, the database is not updated
DC1. SubmitChanges ();
Show additional success
foreach (var g in Dc1.tb_guestinfo)
{
Console.WriteLine ("{0} {1} {2} {3}", G.id, G.name, G.age, G.tel);
}
Console.WriteLine ("---------");
Show database is not updated
Dataclasses1datacontext DC2 = new Dataclasses1datacontext ();
foreach (var g in Dc2.tb_guestinfo)
{
Console.WriteLine ("{0} {1} {2} {3}", G.id, G.name, G.age, G.tel);
}
Console.WriteLine ("------------------------");
Console.readkey ();

2.InsertOnSubmit
Dc2.tb_GuestInfo.InsertOnSubmit (Guset);
DC2. SubmitChanges ();

foreach (var g in Dc2.tb_guestinfo)
{
Console.WriteLine ("{0} {1} {2} {3}", G.id, G.name, G.age, G.tel);
}
Console.WriteLine ("------------------------");
Console.readkey ();
2b. Insertallonsubmit inserting a collection
list<tb_guestinfo> lst = new list<tb_guestinfo> ()
{
New Tb_guestinfo () {name= "AA", age=25,tel= "133****3333"},
New Tb_guestinfo () {name= "BB", age=25,tel= "135****5555"}
};
Dc2.tb_GuestInfo.InsertAllOnSubmit (LST);
DC2. SubmitChanges ();

foreach (var g in Dc2.tb_guestinfo)
{
Console.WriteLine ("{0} {1} {2} {3}", G.id, G.name, G.age, G.tel);
}
Console.WriteLine ("------------------------");
Console.readkey ();
//
3.DeleteOnSubmit
Tb_guestinfo entity = (from G in Dc2.tb_guestinfo
where g.name = = "AA"
Select g). Single ();
Dc2.tb_GuestInfo.DeleteOnSubmit (entity);//
DC2. SubmitChanges ();

foreach (var g in Dc2.tb_guestinfo)
{
Console.WriteLine ("{0} {1} {2} {3}", G.id, G.name, G.age, G.tel);
}
Console.WriteLine ("------------------------");
Console.readkey ();
3b. Deleteallonsubmit
Ienumerable<tb_guestinfo> Entitys = from G in Dc2.tb_guestinfo
where g.name = = "AA" | | G.name = = "BB"
Select G;
Dc2.tb_GuestInfo.DeleteAllOnSubmit (Entitys);
DC2. SubmitChanges ();

foreach (var g in Dc2.tb_guestinfo)
{
Console.WriteLine ("{0} {1} {2} {3}", G.id, G.name, G.age, G.tel);
}
Console.WriteLine ("------------------------");
Console.readkey ();

}
}
}

The results of the program run as follows:

c#→ about the table<tentity> generic class under System.Data.Linq

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.