c# - Simple LINQ query to Delete From DataContext Where ID == ID -


i getting id of entry in database table in codebehind, , trying find simple linq query delete entry based on id.

here have:

datacontext.items.deleteobject((item)datacontext.items.where(item => item.itemid == selectedid)); 

this gives me casting error however, , wondering if there better way accomplish this? i've looked @ similar questions , every answer see seems more complicated should be, suggestions great!

you have iqueryable<item>, not , item - use single() item itself:

var item = datacontext.items.where(item => item.itemid == selectedid).single(); datacontext.items.deleteobject(item); 

this assumes single matching item (id primary key), otherwise consider using first() of firstordefault() null check instead or if have collection of item, delete them in loop:

var items = datacontext.items.where(item => item.itemid == selectedid); foreach(var item in items)   datacontext.items.deleteobject(item); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -