How to delete in LINQ

Posted by Pcjoshi under ASP.NET on 10/11/2012 | Points: 10 | Views : 666 | Status : [Member] | Replies : 2
hello ,
I am trying to delete linq but i am not able to do can u help me?


Posted by: Ranjeet_8 on: 10/12/2012 [Member] [MVP] Gold | Points: 25

try this,

// LINQ Object
DataUsersClassesDataContext dataUsers = new DataUsersClassesDataContext();
// Database object, find the record to delete
ThisUser user = dataUsers.Users.Single(u => u.UserID == XXXXX);
dataUsers.Users.DeleteOnSubmit(user); // not physically deleted, it is marked as deleted but still pending to delete
dataUsers.SubmitChanges(); // physically delete from database

Pcjoshi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 10/12/2012 [Member] Starter | Points: 25

Hi Pcjoshi,

LINQ -- Language integrated query. It is basically used to filter the collection. It is read only.
If you want to make any changes in the data source of the linq (collection) like new element to be added to list, update or deleted. make the necessary changes to the list.


Here, i took a Textbreakclass.cs Class with some properties.

public class Textbreakclass
public int AutoId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public Int64 Phone { get; set; }

crudoperationsinlinq.aspx Page:

<asp:GridView ID="grid1" runat="server" />
<asp:GridView ID="grid2" runat="server" />
<asp:GridView ID="grid3" runat="server" />


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class crudoperationsinlinq : System.Web.UI.Page

string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)

private void GetData()
IList<Textbreakclass> list = GetSampleList();
grid1.DataSource = list;
var list1 = from list2 in list
select list2;
grid2.DataSource = list1;
var list3 = from list4 in list
select list4;
grid3.DataSource = list3;

private IList<Textbreakclass> GetSampleList()
IList<Textbreakclass> list = new List<Textbreakclass>();
string sql = "SELECT * FROM SampleForTutorials ORDER BY AutoId DESC";
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(_connStr))
using (SqlCommand cmd = new SqlCommand(sql, conn))
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
foreach (DataRow row in table.Rows)
list.Add(new Textbreakclass()
Address = row["Address"].ToString(),
AutoId = int.Parse(row["AutoId"].ToString()),
City = row["City"].ToString(),
Name = row["Name"].ToString(),
Phone = Int64.Parse(row["Phone"].ToString())
return list;

The first gridview takes list collection as datasource.
The second gridview takes the list1 as data source (list1 is the linq queried list),
Now, a element is removed from list.
The third gridview takes the list2 as data source(list2 is the filtered linq query list).
In this way, you can delete from list not directly in linq.


Awesome Coding !! :)

Pcjoshi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response