How to understand Linq data and objects
This article mainly explains "how to understand Linq data and objects". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand Linq data and objects".
As we all know, when developing data, the data is generally relational data, but what is the relationship between data and objects? linq is mainly to solve the problem that data is not equal to objects. With Linq data and objects, there can be an one-to-one correspondence. And these are the code that can generate this kind of mapping according to the database, and it can also generate the database according to the mapping code. That is to say, the database and the mapping code are transformed into each other.
First add the Linq to SQL class, named Northwind.dbml, and then add a data table Customers to the class.
From the Northwind.designer.cs file of the figure, you can see
[System.Data.Linq.Mapping.DatabaseAttribute (Name= "Northwind")]
Public partial class NorthwindDataContext: System.Data.Linq.DataContext
{
[Table (Name= "dbo.Customers")]
Public partial class Customers: INotifyPropertyChanging, INotifyPropertyChanged
{
Private static PropertyChangingEventArgs
EmptyChangingEventArgs = new PropertyChangingEventArgs (String.Empty)
Private string _ CustomerID
You can see that NorthwindDataContext must inherit from the DataContext class, so it is supported by Linq.
Private void Form1_Load (object sender, EventArgs e) {NorthwindDataContext db = new NorthwindDataContext (); var c = from p in db.Customers select p; dataGridView1.DataSource = c;}
Run the effect diagram:
Thank you for reading, the above is the content of "how to understand Linq data and objects". After the study of this article, I believe you have a deeper understanding of how to understand Linq data and objects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!