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

  • Home
  • SEARCH
  • 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 298743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:45:34+00:00 2026-05-12T06:45:34+00:00

This works: var i = (from x in db.Test where x.Id == 1 select

  • 0

This works:

var i = (from x in db.Test
    where x.Id == 1
    select x).First();

db.Test.DeleteOnSubmit(i);
db.SubmitChanges();

I get a cast error for this (int/string):

var i = db.Test.Single(x => x.Id == 1);
db.Test.DeleteOnSubmit(i);
db.SubmitChanges();

I was also able to make an update using Single sucesssfully on the same table. Any clues?

Update

Here’s more of the error message:

Here’s more:

[InvalidCastException: Unable to cast object of type ‘System.Int32’ to type ‘System.String’.]
System.Data.Linq.SingleKeyManager2.TryCreateKeyFromValues(Object[] values, V& v) +134
System.Data.Linq.IdentityCache
2.Find(Object[] keyValues) +57
System.Data.Linq.StandardIdentityManager.Find(MetaType type, Object[] keyValues) +51
System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues) +113
System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance) +235
System.Data.Linq.ChangeProcessor.BuildEdgeMaps() +510
System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +137
System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +453
System.Data.Linq.DataContext.SubmitChanges() +38

  • 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-12T06:45:34+00:00Added an answer on May 12, 2026 at 6:45 am

    What’s the data type of your id field?

    I just ran the following code against a dummy database that has data in it where my key field is of type int:

    using (var db = new dbDataContext())
    {
        var x = db.items.Single(i => i.fld_key == 1);
        db.items.DeleteOnSubmit(x);
        db.SubmitChanges();
    
        var y = (from i in db.items
                 where i.fld_key == 2
                 select i).First();
        db.items.DeleteOnSubmit(y);
        db.SubmitChanges();
    }
    

    I had no problems with either, and checking the database, items 1 and 2 were both deleted successfully.

    Are you attempting to delete an item that doesn’t exist in the second code block – as you deleted it with the first. This would mean that y is actually null and you’re attempting to DeleteOnSubmit passing null which will give you an invalid cast as you can’t cast null.

    Try attempting to delete a different item in your second code block, I would imagine it would work in that case.

    Change .Single to .SingleOrDefault and wrap your delete lines to check for null:

    if(null != x)
    {
        db.items.DeleteOnSubmit(x);
        db.SubmitChanges();
    }
    

    I would wager this will fix the problem you’re seeing.

    Edit Looking at the exception you’ve posted, I’m wondering if you changed the data type of your table to int after you generated your dbml. Reload the table into your dbml and see if that fixes the issue. Either that, or the issue is the other way around and you changed it to VarChar/NVarChar and haven’t updated your dbml.

    I would definitely take a look at your entities and make sure that the data types match those in your data table in your underlying database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 142k
  • Answers 142k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is a Tag property in Windows Forms, but it… May 12, 2026 at 8:14 am
  • Editorial Team
    Editorial Team added an answer Personal expierience with the RichTextBox learned me that this control… May 12, 2026 at 8:14 am
  • Editorial Team
    Editorial Team added an answer Try: var coll = new int[] { 1, 2, 3,… May 12, 2026 at 8:14 am

Related Questions

I am building application that required some data from iPhone's Call log(read only). The
I m just starting with microsoft (MVC,C#, LINQ), so far so good, but i
I have a project where I am taking some particularly ugly live HTML and
The following method is pretty simple, I'm trying to determine a line-item rate by
Caution: This question is over nine years old! Your best option is to search

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.