'Delete query in Linq
I have this simple code but it shows error. I dont know where I am going wrong. It shows error in last line.
linq_testDataContext db = new linq_testDataContext();
var remove = from aremove in db.logins
where aremove.username == userNameString && aremove.Password == pwdString
select aremove;
db.logins.DeleteOnSubmit(remove);
Solution 1:[1]
DeleteOnSubmit takes a single object. You are passing an IEnumerable<login> to it. Use DeleteAllOnSubmit instead, or select a single object from your resulting collection, like this:
var remove = (from aremove in db.logins
where aremove.username == userNameString
&& aremove.Password == pwdString
select aremove).FirstOrDefault();
if(remove != null)
{
db.logins.DeleteOnSubmit(remove);
}
Solution 2:[2]
you are missing foreach loop to delete all entity.
Use like below
var remove = from aremove in db.logins
where aremove.username == userNameString && aremove.Password == pwdString
select aremove;
and after that
foreach (var detail in remove)
{
db.logins.DeleteOnSubmit(detail);
}
and at last
try
{
db.SubmitChanges();
}
catch (Exception e)
{
// Provide for exceptions.
}
Hope this will help you.
Solution 3:[3]
Instead of:
db.logins.DeleteOnSubmit(remove);
Call DeleteAllOnSubmit(), like this:
db.logins.DeleteAllOnSubmit(remove.ToList());
Make sure to call db.SubmitChanges() afterwards. You could use .AsEnumerable() as well, either or. If it's a large delete operation though, you may want to think about bypassing LINQ in this case.
Solution 4:[4]
use this code its work
var remove = (from aremove in db.logins
where aremove.username == userNameString
&& aremove.Password == pwdString
select aremove).FirstOrDefault();
if(remove != null)
{
db.logins.Remove(remove);
db.SaveChanges();
}
Solution 5:[5]
try using the method first(), something like this:
var remove =
(from aremove in db.logins where aremove.username == userNameString
&& aremove.Password == pwdString select aremove).first();
db.logins.DeleteOnSubmit(remove);
Solution 6:[6]
For good practices
private void DeleteCourse()
{
int id = Convert.ToInt32( txtSearch.Text);
CourseDemoDataContext cdContext = new CourseDemoDataContext();
course courseobj = cdContext.courses.Single(courses => courses.COURSE_ID == id);
cdContext.courses.DeleteOnSubmit(courseobj);
cdContext.SubmitChanges();
}
Solution 7:[7]
You can try this, It will be worked for you.
linq_testDataContext _DbContext = new linq_testDataContext();
var remove = _DbContext.aremove.where(x=>x.username == userNameString && x.Password == pwdString).FirstOrDefault();
if(remove != null)
{
db.logins.Remove(remove);
db.SaveChanges();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Philip Daubmeier |
| Solution 2 | Mukesh Kumar |
| Solution 3 | Nick Craver |
| Solution 4 | shebin c babu |
| Solution 5 | skamlet |
| Solution 6 | sarder kamruzzaman polash |
| Solution 7 | Qais Ali Abbas |
