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 8705503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:22:36+00:00 2026-06-13T03:22:36+00:00

My goal is to make a deletion function that uses LINQ to delete a

  • 0

My goal is to make a deletion function that uses LINQ to delete a record from a database table by finding the id the user inputs that matches the record’s primary key.

  • MyDBEntitiesClass is the argument that the function needs to be passed which tells it what DataTable to search.

  • IDontKnowWhatType is the type I’m asking for help in determining.

  • MyPrimaryKey needs to be replaced with something that references the primary key of MyDBEntitiesClass.

  • TestDatabaseEntities is the name of the connection string created by generating an entity model from my existing SQL server database.

public static void DeleteFrom(IDontKnowWhatType MyDBEntitiesClass)
{

var dbEntities = new TestDatabaseEntities(); 

    //Prompt the user for an ID
    Console.WriteLine("Select ID for deletion.");

    // save the ID to IDtoMark
    int IDtoMark = int.Parse(Console.ReadLine());
    try
    {
        // return a single record that matches IDtoMark 
        MyDbEntitiesClass x = dbEntities.MyDbEntitiesClass.Single(x => x.myPrimaryKey == IDtoMark);
        //Remove the marked record, and save changes
        dbEntities.MyDbEntitiesClass.Remove(x);
        dbEntities.SaveChanges();
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
  • 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-06-13T03:22:38+00:00Added an answer on June 13, 2026 at 3:22 am

    You can achieve this by implementing Repository Pattern.

    I guess you are using entity framework. For that you have generic class.

    public class Repository<T> where T : class
    {
        private DbContext dbContext;
        private DbSet<T> DbSet { get; set }
        public Repository(dbContext)
        {
           this.dbContext = dbContext;
           this.DbSet = this.dbContext.DbSet<T>();
        }
    
        public void Delete(int id)
        {
             T entity = DbSet.Find(id);
             DbSet.Remove(entity);
             dbContext.Save();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to make a modern webapp that uses location hash for navigation
Goal is to make a dialog that appears on menu_key pressed, but it keeps
My goal is to make a layout that is 200% width and height, with
Goal: Produce an Excel document with information from 3 associated models that is similar
Goal: Make a decorator which can modify the scope that it is used in.
My goal is to make an apprequest button in my app that invites friends.
JSFiddle: http://jsfiddle.net/M2ALY/3/ My goal is to make a module that I can use and
My goal is to make the Delete key (without any modifiers) move selected files
My goal is to make a flash/as3 program that would pull multiple sets of
My goal is to make screenshot from window without displaying it. Code: All the

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.