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

The Archive Base Latest Questions

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

I know it’s a bizarre title, and i know inheritance is not possible with

  • 0

I know it’s a bizarre title, and i know inheritance is not possible with enums, so let me explain.

If i have the following classes:

  • Fruit (abstract)
    • Apple (concrete – derived from Fruit)
    • Orange (concrete – derived from Fruit)

And i have the following method, implemented with generics:

public ICollection<T> FindFruit<T>() where T : Fruit
{
   return _fruitRepository
             .Fruits
             .OfType<T>()
             .ToList();
}

And i use it like this:

var apples = _fruitServices.FindFruit<Apple>();

All fine.

Now – i currently have the following enum:

public enum FruitAssociations
{
   Color,
   Manufacturers
}

Basically a “Fruit” can have many associations, which designates what associations to include in the result from my repository.

So the FindFruit method above is actually like this:

public ICollection<T> FindFruit<T>(FruitAssociations[] assocs) where T : Fruit
{
   return _fruitRepository
             .Fruits
             .OfType<T>()
             .IncludeAssocs(assocs) // ObjectQuery<Fruit> extension method.
             .ToList();
}

Here is that extension method:

public static ObjectQuery<Fruit> IncludeAssocs(this ObjectQuery<Fruit> source, FruitAssociations[] assocs)
{
   if (assocs.Contains(FruitAssociations.Color))
      query = query.Include("Color");
   // etc      
}

Also works fine. The problem i am now facing however is i have associations on particular Fruit.

E.g

public enum OrangeAssociations
{
   OrangeFamilies
}

And i’m not sure how to have a single FindFruit method that is capable of returning associations based on the type of T.

This is the end result i would like to be able to do:

var oranges = _fruitServices.FindFruit<Orange>(new[] { OrangeAssociations.OrangeFamilies });
var apples = _fruitServices.FindFruit<Apple>(new[] { AppleAssociations.AppleFamilies });

But i can’t figure out how to do that without having seperate FindFruit methods for each fruit type, and hence defeating the point of the generic T type parameter.

For those who are curious as to what i’m doing here – i’m using eager loading with Entity Framework 4, and therefore using the .Include method (which takes a string designating the navigational property). So my service accepts an array of enumerations for a given entity, then i use an extension method to translate that to a string used in the .Include statement.

I’m thinking the solution is to instead of accepting an array of enums, accept a generic class:

public ICollection<T> FindFruit<T>(FruitAssociations<T> assocs) where T : Fruit

And use it like this:

var associations = new FruitAssociations<Orange>(OrangeAssociations.OrangeFamilies);
var apples = _fruitServices.FindFruit<Apple>(associations);

But i’m not sure how that would work, or how to implement it.

Hopefully my question makes sense and is not too long/complicated.

  • 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-18T12:41:47+00:00Added an answer on May 18, 2026 at 12:41 pm

    Eric Lippert will come to my house and beat me with a stick for this, but it works.

    Usage:

    // finds fruits of type Orange, includes Color and OrangeFamilies
    var result = FindFruit(OrangeAssociation.Color,
                           OrangeAssociation.OrangeFamilies);
    

    or

    // finds fruits of type Fruit, includes Manufacturers
    var result = FindFruit(FruitAssociation.Manufacturers);
    

    Implementation:

    static ICollection<TFruit> FindFruit<TAssoc, TFruit>(
        params FruitAssociation<TAssoc, TFruit>[] assocs)
        where TAssoc : FruitAssociation<TAssoc, TFruit>, new()
        where TFruit : Fruit
    {
        var query = _fruitRepository.Fruits.OfType<TFruit>();
    
        foreach (var assoc in assocs)
        {
            query = query.Include(assoc.Name);
        }
    
        return query.ToList();
    }
    

    with

    abstract class FruitAssociation<TAssoc, TFruit>
        where TAssoc : FruitAssociation<TAssoc, TFruit>, new()
        where TFruit : Fruit
    {
        public static readonly TAssoc Color = Define("Color");
    
        public static readonly TAssoc Manufacturers = Define("Manufacturers");
    
        protected static TAssoc Define(string name)
        {
            return new TAssoc { Name = name };
        }
    
        public string Name
        {
            get;
            private set;
        }
    }
    
    sealed class FruitAssociation : FruitAssociation<FruitAssociation, Fruit>
    {
    }
    
    sealed class OrangeAssociation : FruitAssociation<OrangeAssociation, Orange>
    {
        public static readonly OrangeAssociation OrangeFamilies =
            Define("OrangeFamilies");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone know if it's possible to databind the ScaleX and ScaleY of a render
I know, I would have thought the answer was obviously no as well, but
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
Know of any good libraries for this? I did some searches and didn't come
I know in certain circumstances, such as long running processes, it is important to
I know Microsoft has made efforts in the direction of semantic and cross-browser compliant
I know that I can do something like $int = (int)99; //(int) has a
I know this might be a no-brainer, but please read on. I also know
I know there is a registry key indicating the install directory, but I don't
I know I must be missing something, but in a while statement how does

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.