Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:05:41+00:00 2026-05-23T12:05:41+00:00

I am getting the ID of an entry in a database table in my

  • 0

I am getting the ID of an entry in a database table in my codebehind, and am trying to find a simple LINQ query to delete the entry based on the ID.

Here is what I have:

DataContext.Items.DeleteObject((Item)DataContext.Items.Where(item => item.ItemId == selectedId));

This gives me a casting error however, and I am wondering if there is a better way to accomplish this? I’ve looked at similar questions and every answer I see seems to be more complicated than this should actually be, so any suggestions would be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:05:42+00:00Added an answer on May 23, 2026 at 12:05 pm

    You currently have an IQueryable<Item>, not and Item – use Single() to get the item itself:

    var item = DataContext.Items.Where(item => item.ItemId == selectedId).Single();
    DataContext.Items.DeleteObject(item);
    

    This assumes a single matching item (ID as primary key), otherwise consider using First() of FirstOrDefault() with a null check instead or if you have a collection of item, just delete them in a loop:

    var items = DataContext.Items.Where(item => item.ItemId == selectedId);
    foreach(var item in items)
      DataContext.Items.DeleteObject(item);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to delete a database entry by sending the primary key userId
I'm trying to run a query on a very simple table but I'm not
Getting extremely confused with an adminhtml module i'm trying to write! Effectively I have
I'm Using Camick's ListTableModel and RowTableModel from here http://tips4java.wordpress.com/2009/03/12/table-from-database/ However, the JTable is not
Say I have a database table with three fields: id, content and timestamp. Entries
Hy there, i have some tree-structured data in my sqlite-table and want to delete
I am trying to insert an entry into a database using a form created
I have a foxpro database that we are trying to write to using MS
I have a simple address book type of database. I've probably overnormalised, but it's
I'm getting data into my database without any problem using mysql_real_escape_string. So an entry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.