Anonymous Data Type In MVC

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:

new

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.

application

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:

add

After clicking on Add button go to Server Explorer and establish the connection as in the following figure:

Explorer

After establishing the connection select the table and drop to DataClass1.dbml as in the following figure:

table

Go to Form design and design the form, then go to form.cs and write the following code,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace LinqEmployeeDetails  
  12. {  
  13.     public partial class Form1: Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {  
  21.   
  22.         }  
  23.         DataClasses1DataContext objbe = new DataClasses1DataContext();  
  24.         private void Form1_Load(object sender, EventArgs e) {  
  25.             // dataGridView1.DataSource = objbe.EmployeeDetails.ToList();  
  26.         }  
  27.   
  28.         private void buttonInsert_Click(object sender, EventArgs e)   
  29.         {  
  30.             EmployeeDetail objed = new EmployeeDetail();  
  31.             objed.Id = Convert.ToInt32(textBoxId.Text);  
  32.             objed.Name = textBoxName.Text;  
  33.             objed.Designation = textBoxDesignation.Text;  
  34.             objbe.EmployeeDetails.InsertOnSubmit(objed);  
  35.             objbe.SubmitChanges();  
  36.             MessageBox.Show("Data has Inserted");  
  37.             dataGridView1.DataSource = objbe.EmployeeDetails.ToList();  
  38.         }  
  39.         private void btnUpdate_Click(object sender, EventArgs e)  
  40.         {  
  41.             var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First(); //Using Var as Anonumous Type  
  42.             rec.EName = txtEName.Text;  
  43.             rec.EDesignation = txtDesignation.Text;  
  44.             rec.ESalary = Convert.ToInt32(txtESalary.Text);  
  45.             objdc.SubmitChanges();  
  46.             MessageBox.Show("Record is Updated");  
  47.         }  
  48.         private void btnDelete_Click(object sender, EventArgs e)  
  49.         {  
  50.             var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First(); //Using Var as Anonumous Type  
  51.   
  52.             objdc.Employee.DeleteOnSubmit();  
  53.             objdc.SubmitChanges();  
  54.             MessageBox.Show("Record is Deleted");  
  55.         }  
  56.         private void btnFind_Click(object sender, EventArgs e)  
  57.         {  
  58.             var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First(); //Using Var as Anonumous Type  
  59.   
  60.             txtEName.Text = rec.EName;  
  61.             txtDesignation.Text = rec.EDesignation;  
  62.             txtESalary.Text = rec.ESalary.ToString();  
  63.         }  
  64.   
  65.     }  
  66. }  


Similar Articles