c# - How to delete a row from database using lambda linq? -


i want perform delete operation unfriend user in android application i'm developing. following method returns "done" data doesn't delete table. problem here?

public string deletefriend(int user, int friend) {     int = db.friends.where(x => x.person.id == user && x.person1.id == friend).select(x => x.id).first();                var rec=db.friends.tolist();     rec.removeall(x=>rec.any(xx=>xx.id==i));     db.savechanges();     return "done"; } 

i'm working entity frame work linq.

try this:

var friend = db.friends.where (x=>x.person.id == user && x.person1.id == friend).firstordefault(); if (friend != null)  {    db.friends.remove(friend);    db.savechanges()  } 

if have got multiple records can rid of firstordefault , add .tolist() in end. , use db.friends.removerange(friend)

it cleaner , hope helps

thanks


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -