Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Linq Queries

Linq Queries

Scheduled Pinned Locked Moved LINQ
csharplinqquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hahii
    wrote on last edited by
    #1

    Actually I am very new to Linq. But I have a project where I have to use linq to entities. I have a Gridview in my aspx page and I have a table named Address with 6 columns. I have to bound this Gridview with data from table and I have to delete, update and insert new records using linq quiereies. How can I do that. I really appreciate that. Regards,

    N 1 Reply Last reply
    0
    • H hahii

      Actually I am very new to Linq. But I have a project where I have to use linq to entities. I have a Gridview in my aspx page and I have a table named Address with 6 columns. I have to bound this Gridview with data from table and I have to delete, update and insert new records using linq quiereies. How can I do that. I really appreciate that. Regards,

      N Offline
      N Offline
      Niladri_Biswas
      wrote on last edited by
      #2

      It is very easy to do. In such situations, any kind of entity framework will help you. e.g. Ado.net Entity Framework, n-hibernate. I am presenting a sample pseudo code for that in Ado.net Entity Framework A) Fetch operation

      public List<Employee> FetchRecord()
      {
      EmployeeInfoEntities objEmployeeInfoEntities = new EmployeeInfoEntities();
      ObjectQuery<Employee> employeeQuery = objEmployeeInfoEntities.Employee;
      return employeeQuery.ToList();
      }

      B) Insert operation

      public void AddEmployee(Employee e1)
      {
      try
      {
      EmployeeInfoEntities objEmployeeInfoEntities = new EmployeeInfoEntities();

                 objEmployeeInfoEntities.AddToEmployee(e1);
                 
                 objEmployeeInfoEntities.SaveChanges();
                
             }
             catch (Exception e)
             {
                 string msg = e.Message;
             }
         }
      

      c) Update Operation

      public void UpdateEmployee(Employee e1)
      {
      try
      {
      EmployeeInfoEntities objEmployeeInfoEntities = new EmployeeInfoEntities();

                 Employee e2=(
                                  from emp in objEmployeeInfoEntities.Employee 
                                  where emp.EmployeeID == e1.EmployeeID
                                  select emp
                              ).First();
                 e2.Name = e1.Name;
                 e2.Title = e1.Title;
                 e2.Gender = e1.Gender;
                 objEmployeeInfoEntities.SaveChanges();
             }
             catch (Exception e)
             {
                 string msg = e.Message;
             }
      
         }
      

      D) Delete operation

      public void DeleteEmployee(Employee e1)
      {
      try
      {

                 EmployeeInfoEntities objEmployeeInfoEntities = new EmployeeInfoEntities();
                 
      
                 Employee e2 = (
                                  from emp in objEmployeeInfoEntities.Employee
                                  where emp.EmployeeID == e1.EmployeeID
                                  select emp
                              ).First();
      
                 objEmployeeInfoEntities.DeleteObject(e2);
                 objEmployeeInfoEntities.SaveChanges();
             }
             catch (Exception e)
             {
                 string msg = e.Message;
             }
         }
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups