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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:21:03+00:00 2026-05-24T13:21:03+00:00

I have a number of LINQ classes that inherit from the same base class.

  • 0

I have a number of LINQ classes that inherit from the same base class. Each Linq class has its own table, and then the base class is a sort of link to another table. I’ll try and illustrate the relationship:

//Base Class
abstract class Element<T> 
{
   public string Name {get; set;}
   public string Species {get; set;}
}

public partial class DogElement : Element<DogElement>
{
   public int Legs {get; set;}
   public bool hasHair {get; set;}
}

public partial class CatElement : Element<CatElement>
{
   public bool hasWiskers {get; set}
}

So DogElement and CatElement are separate physical tables in the database. The Species class will read from the Species table. I basically want to have a generic function in Element that groups the T table with the Species table and returns the result as a T after having set .Species.

//Assume T is DogElement for this example
public IQueryable<T> Collection
{
   using (DataBase db = new DataBase())
   {
     var k = from t in db.GetTable<T>()
             where t.SomeID == SomeOtherID
             select t;

     k = { {Legs = 4, HasHair = True, Species = <null>, Name = <null>}, ...}

     I basically want a query that will return an IQueryable<DogElement> with Name
     and Species set from a join query.  What is the best way to set the Name and 
     Species values?  

   return k;
} 
  • 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-24T13:21:04+00:00Added an answer on May 24, 2026 at 1:21 pm

    Essentially you are asking SQL to handle inheritance (it doesn’t do that)… and LINQ doesn’t model inheritance either. A good object relational mapper (ORM) will help you piece something together more generically.

    That said, here’s a quick discussion.

    First off, I think it is better to write one LINQ query for each species-animal pair.

    You could write a union, switch in the LINQ select, create a new animal, and then cast that animal to a species in one go… then outside the LINQ statement, recast it to T on the way out. This kind of statement sounds messy though.

    Although this is less efficient than one big union statement, have you considered this more maintainable option?

    var a from speciies select ..
    
    if(T is Dog){
      additional linq statement}
    else if(T is Cat){
      additional linq statement}
    

    that’s two trips to the database (slower), but it’s more maintainable, and often it is better not to over optimise your code on the first pass.

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

Sidebar

Related Questions

I have a LINQ query that uses 1 table + a large number of
I have a number of LINQ to SQL tables with the same fields (
I have a method that counts the number of Contacts each Supplier, Customer and
Using Visual Studio's O/R Designer, I have created a number of classes the inherit
Say I have an arbitrary number of collections, each containing objects of the same
i have a number of lambda expressions that will be using the same predicate
I have array number = {2,3,4,5,6} Now i have to select rows from table
I have a linq statement that searches a number of fields based on user
Although I have been through a number of posts that discuss Linq to SQL
I have a linq union statement that has been giving me some trouble and

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.