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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:19:28+00:00 2026-05-16T01:19:28+00:00

Come across a problem with the repository pattern combined with the use of abstract

  • 0

Come across a problem with the repository pattern combined with the use of abstract classes.

I have a repository which implements a single method returning an ICollection of an abstract type.

Here’s my abstract class:

public abstract class Location
{
   public abstract string Name { get; set; }
   public abstract LocationType Type { get; }
}

Here’s a concrete implementation of that abstract class:

public class Country : Location
{
   public override string Name { get; set; }
   public override LocationType Type { get { return LocationType.Country; } }
}

Here’s my repository:

public class LocationsRepository : Locations.Repository.ILocationsRepository
{
   public ICollection<Location> GetAllLocations()
   {
      Country america = new Country { Name = "United States" };
      Country australia = new Country { Name = "Australia" };
      State california = new State { Name = "California", Country = america };

      return new List<Location>() { america, australia, california };
    }
}

All good so far.

Now the service:

public class CountryService : ICountryService
{
   private ILocationsRepository repository;

   public CountryService()
   {
      // in reality this is done by DI, but made 'greedy' for simplicity.
      this.repository = new LocationsRepository();
   }

   public List<Country> GetAllCountries()
   {
      // errors thrown by compiler
      return repository.GetAllLocations()
                       .Where(l => l.Type == LocationType.Country)
                       .ToList<Country>();
   }
}

There’s the problem. I’m trying to return a list of concrete types (Country) from a repository which returns an ICollection<T> of an abstract type.

Get 2 compile-time errors:

‘System.Collections.Generic.IEnumerable’
does not contain a definition for
‘ToList’ and the best extension method
overload
‘System.Linq.ParallelEnumerable.ToList(System.Linq.ParallelQuery)’ has some invalid arguments

and

Instance argument: cannot convert from
‘System.Collections.Generic.IEnumerable’
to
‘System.Linq.ParallelQuery’

So, how can i implement this pattern?

I can kind of understand the issue (you can’t instantiate an abstract type), so does the Enumerator (.ToList) attempt to instantiate this, hence the error?

In case you don’t understand what im trying to do:

  • I want my repository to return an ICollection<T> of an abstract type
  • I want my services (i will have one for each concrete type) to return a List of concrete types based on that single repository method

Is this just a case of LINQ syntax? Or is my design pattern totally wrong?

  • 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-16T01:19:28+00:00Added an answer on May 16, 2026 at 1:19 am
    repository.GetAllLocations().OfType<Country>().ToList();
    

    And you don’t even need the LocationType enum

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

Sidebar

Related Questions

I have come across a strange problem which I would like to get your
I frequently come across this problem. I have a file: something something2 something3 which
I'm rather new to JAXB and have come across a problem which I can't
I am teaching myself some Python and I have come across a problem which
I have come across this problem several times in which I would like to
I am currently dabbling in jQuery and have come across a problem which I
I have come across a problem. There are N piles of stones where the
I am new to sql and I have come across a problem on joining
I am using $SUB for the first time and have come across this problem.
I'm fairly new to LWJGL/OpenGL and I've come across this problem which I can't

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.