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. checking duplicate rows using linq while inserting data.........

checking duplicate rows using linq while inserting data.........

Scheduled Pinned Locked Moved LINQ
helpcsharpdatabaselinqquestion
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.
  • P Offline
    P Offline
    pranavcool
    wrote on last edited by
    #1

    Hi, I am using LINQ for the first time.I have written a code in LINQ that will insert employees details in to the database. Now the problem is that i do not want to insert duplicate data(employee details) in to the database. How do i check duplicate data using LINQ before new inserting data. I want to check the first name and last name of employee in table before inserting new data.If the record(e.g first name and last name) exist then it will show some message.. The following is the code that i am using to insert data in to the table....

    EmployeesDetailsDataContext DetailContext = new EmployeesDetailsDataContext();
    Employee tblemp = new Employee();
    tblemp.Emp_Code = txtEmpId.Text.Trim();
    tblemp.F_Name = txtFname.Text.Trim();
    tblemp.L_Name = txtLname.Text.Trim();
    tblemp.Country = Convert.ToInt32(ddlCountry.SelectedValue);
    tblemp.State = Convert.ToInt32(ddlRegion.SelectedValue);
    tblemp.City = Convert.ToInt32(ddlCity.SelectedValue);
    tblemp.Designation = txtDes.Text.Trim();
    tblemp.Address = txtAdd.Text.Trim();
    DetailContext.Employees.InsertOnSubmit(tblemp);
    DetailContext.SubmitChanges();

    any help would be appreciate

    Regards, Pranav Dave

    S 1 Reply Last reply
    0
    • P pranavcool

      Hi, I am using LINQ for the first time.I have written a code in LINQ that will insert employees details in to the database. Now the problem is that i do not want to insert duplicate data(employee details) in to the database. How do i check duplicate data using LINQ before new inserting data. I want to check the first name and last name of employee in table before inserting new data.If the record(e.g first name and last name) exist then it will show some message.. The following is the code that i am using to insert data in to the table....

      EmployeesDetailsDataContext DetailContext = new EmployeesDetailsDataContext();
      Employee tblemp = new Employee();
      tblemp.Emp_Code = txtEmpId.Text.Trim();
      tblemp.F_Name = txtFname.Text.Trim();
      tblemp.L_Name = txtLname.Text.Trim();
      tblemp.Country = Convert.ToInt32(ddlCountry.SelectedValue);
      tblemp.State = Convert.ToInt32(ddlRegion.SelectedValue);
      tblemp.City = Convert.ToInt32(ddlCity.SelectedValue);
      tblemp.Designation = txtDes.Text.Trim();
      tblemp.Address = txtAdd.Text.Trim();
      DetailContext.Employees.InsertOnSubmit(tblemp);
      DetailContext.SubmitChanges();

      any help would be appreciate

      Regards, Pranav Dave

      S Offline
      S Offline
      Syed Mehroz Alam
      wrote on last edited by
      #2

      Something like this might help:

      EmployeesDetailsDataContext DetailContext = new EmployeesDetailsDataContext();

      //check if there exists a record with the same First and Last Name
      bool exists = DetailContext.Employees.Any ( e => e.F_Name == txtFname.Text.Trim() && e.L_Name == txtLname.Text.Trim() )

      if (exists)
      //display message
      else
      //insert record

      Regards, Syed Mehroz Alam

      My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

      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