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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:09:06+00:00 2026-06-08T20:09:06+00:00

I have a class heirarсhy: using System; using System.Collections.Generic; using System.Linq; using System.Web; using

  • 0

I have a class heirarсhy:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyProject.Models;
using MyProject.Extensions;

namespace MyProject.Models
{
    public abstract class Vehicle
    {
        public string Color { get; set; }
    }

    public class Car : Vehicle
    {
        public int Mileage { get; set; }
    }

    public class Boat : Vehicle
    {   
        public int Displacement { get; set; }
    }

}


namespace MyProject.Extensions
{
    public static class VehicleExtensions
    {
        public static IEnumerable<Vehicle> FilterByColor(this IEnumerable<Vehicle> source, string color)
        {
            return source.Where(q => q.Color == color);
        }
    }
}

namespace MyProject.Controllers
{
    public class IndexController : Controller
    {
        public ActionResult Index()
        {
            List<Car> cars = new List<Car>();
            cars.Add(new Car() { Color = "white", Mileage = 10000 });
            cars.Add(new Car() { Color = "black", Mileage = 20000 });

            IEnumerable<Car> filtered = cars.FilterByColor("black");        
                // Compile error, can not cast IEnumerable<Vehicle> to IEnumerable<Car>

                //.OfType<Car>() - only this helps. I`m looking for another ways

            return View(filtered);
        }
    }
}

I want to use extension method on IEnumerable<Car> and get IEnumerable<Car> from it but method returns IEnumerable<Vehicle>, because it works across all derived classes – compile error. Only one way I know to fix this is to add call .OfType<Car>(), but is this a preferred way? May be there are better ways?

  • 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-06-08T20:09:07+00:00Added an answer on June 8, 2026 at 8:09 pm

    I suspect you just want to make it a generic method with a constraint to ensure that the type parameter is Vehicle or a subclass:

    public static IEnumerable<T> FilterByColor<T>(this IEnumerable<T> source, 
        string color) where T : Vehicle
    {
        return source.Where(q => q.Color == color);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom ValidationSummary helper: namespace System.Web.Mvc.Html { using System; using System.Collections.Generic; using
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
For example, I have this class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
I have a model class similar to the following: using System; using System.Collections.Generic; using
I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
I have a repository class and it has this, using System; using System.Collections.Generic; using
I have the following code in my HomeController.cs using System; using System.Collections.Generic; using System.Linq;
I currently have a class file with the following enumeration: using System; namespace Helper
I have this code for the controller in /Controllers/Cubo/FilterController.cs using System; using System.Collections.Generic; using
i am creating a process using System.DIagnostics.Process in c# i have created a class

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.