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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:04:19+00:00 2026-05-20T21:04:19+00:00

Say I have a database table called Animals which I drag on to my

  • 0

Say I have a database table called Animals which I drag on to my Linq-2-SQL DBML file.

This creates me a class called Animal.

I then create a class to extend it, like so:

public partial class Animal{
 //my extra code here
}

But what I also want to do is have a class like this:

public class Zebra : Animal{
 //even more extra code here
}

The trouble is I get an InvalidCastException from my repository, when I do this:

Animal a = dataContext.Animals.Where(c=>c.id.Equals(id)).SingleOrDefault();
return (Zebra)a;

I’ve stripped back my classes to the point where they’re completely empty in an attempt to work why it doesn’t work. To no avail.

This casting process did used to work in the non-LINQ-2-SQL project that I’m switching to MVC/LINQ.

Done lots of Googling but completely stumped on this one…

Assuming it can’t be done, what the best alternative approach? Interfaces?

Jake

  • 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-20T21:04:19+00:00Added an answer on May 20, 2026 at 9:04 pm

    It doesn’t work because while a Zebra is an Animal, not all Animals are Zebras. When you get the class it doesn’t know anything about the extra methods and properties that belong to class Zebra. If you could cast it as Zebra, it would be missing these.

    What you probably should do is make a constructor on Zebra that takes an Animal and produces a Zebra object from that Animal (if possible).

    return dataContext.Animals
                      .Where( c => c.id == id )
                      .Select( a => new Zebra(a) )
                      .SingleOrDefault();
    

    Alternatively, you can look at using a column discriminator and baking the subclassing into your data context using inheritance mapping.

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

Sidebar

Related Questions

Lets say I have a table in a sql server 2000 database called TransactionType:
I have a field (say, foo) in a table in a SQL Server database
Lets say I have a database table called Scrape possibly setup like: UserID (int)
This question can best be asked by example. Say I have a database table
Say I have a table called myTable. What is the SQL command to return
Let's say I have a table called my_table that looks like this: id |
Say I have a table in a database called TestDB with a table called
Say I have at database table containing information about a news article in each
Let's say I have a database column 'grade' like this: |grade| | 1| |
Let's say you have a database with a single table like... --------------------------------------------- | Name

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.