Introduction
Anonymous type does not contain any name but it implements the methods. Anonymous type is a class type which gets to known about the type of data at the time of compile only. Declaration of variables of anonymous data type is mandatory otherwise it will raise error. Var keyword is used to represent Anonymous type.
Syntax for Anonymous Type
Var q1=(“ from X in x select data field where condition”).First();
Ex
Var Q1=(“ From Emp in x select Emp where eid=Convert.ToInt2(TxtEid.Text)).First();
Implementing Anonymous Data Type in MVC to work with LINQ TO SQL
Step 1: Go to Visual Studio and select New project as in the following figure:
After selecting, New Project windows will appear. Now, after selecting
Windows from installed template select Windows Forms Application and write the Application name, then click on Ok button.
After clicking on Ok button, go to solution explorer and click with right mouse button to Application and then select Add and New Item. After that select LINQ to SQL Classes from Add New Item window. After selecting LINQ to SQL Classes write with extension of dbml. Click on Add button as in the following figure:
After clicking on Add button go to Server Explorer and establish the connection as in the following figure:
After establishing the connection select the table and drop to DataClass1.dbml as in the following figure:
Go to Form design and design the form, then go to form.cs and write the following code,
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace LinqEmployeeDetails
- {
- public partial class Form1: Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
-
- }
- DataClasses1DataContext objbe = new DataClasses1DataContext();
- private void Form1_Load(object sender, EventArgs e) {
-
- }
-
- private void buttonInsert_Click(object sender, EventArgs e)
- {
- EmployeeDetail objed = new EmployeeDetail();
- objed.Id = Convert.ToInt32(textBoxId.Text);
- objed.Name = textBoxName.Text;
- objed.Designation = textBoxDesignation.Text;
- objbe.EmployeeDetails.InsertOnSubmit(objed);
- objbe.SubmitChanges();
- MessageBox.Show("Data has Inserted");
- dataGridView1.DataSource = objbe.EmployeeDetails.ToList();
- }
- private void btnUpdate_Click(object sender, EventArgs e)
- {
- var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First();
- rec.EName = txtEName.Text;
- rec.EDesignation = txtDesignation.Text;
- rec.ESalary = Convert.ToInt32(txtESalary.Text);
- objdc.SubmitChanges();
- MessageBox.Show("Record is Updated");
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First();
-
- objdc.Employee.DeleteOnSubmit();
- objdc.SubmitChanges();
- MessageBox.Show("Record is Deleted");
- }
- private void btnFind_Click(object sender, EventArgs e)
- {
- var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First();
-
- txtEName.Text = rec.EName;
- txtDesignation.Text = rec.EDesignation;
- txtESalary.Text = rec.ESalary.ToString();
- }
-
- }
- }