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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:48:41+00:00 2026-05-14T22:48:41+00:00

I have an abstract class called Grouping. I have a subclass called GroupingNNA. public

  • 0

I have an abstract class called Grouping. I have a subclass called GroupingNNA.

public class GroupingNNA : Grouping {
  // blah blah blah
}

I have a List that contains items of type GroupingNNA, but is actually declared to contain items of type Grouping.

List<Grouping> lstGroupings = new List<Grouping>();
lstGroupings.Add(
  new GroupingNNA { fName = "Joe" });
lstGroupings.Add(
  new GroupingNNA { fName = "Jane" });

The Problem:
The following LINQ query blows up on me because of the fact that lstGroupings is declared as List< Grouping> and fName is a property of GroupingNNA, not Grouping.

var results = from g in lstGroupings
              where r.fName == "Jane"
              select r;

Oh, and this is a compiler error, not a runtime error. Thanks in advance for any help on this one!

More Info:
Here is the actual method that won’t compile. The OfType() fixed the LINQ query, but the compiler doesn’t like the fact that I’m trying to return the anonymous type as a List< Grouping>.

private List<Grouping> ApplyFilterSens(List<Grouping> lstGroupings, string fSens) {

  // This works now! Thanks @Lasse
  var filtered = from r in lstGroupings.OfType<GroupingNNA>()
                 where r.QASensitivity == fSens
                 select r;

  if (filtered != null) {
    **// Compiler doesn't like this now**
    return filtered.ToList<Grouping>();
  }
  else
    return new List<Grouping>();
  }
  • 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-14T22:48:42+00:00Added an answer on May 14, 2026 at 10:48 pm

    Try:

    = from g in lstGroupings.OfType<GroupingNNA>()
    

    this will skip any elements not of type GroupingNNA, and also make the compiler use GroupingNNA as the type for g.

    In response to comment and edited question. No, the compiler will certainly not be happy about your changed collection, but you can fix that:

    return new List<Grouping>(filtered.ToArray());
    

    This relies on the fact that arrays in .NET are co/contra-variant, which allows the compiler to treat GroupingNNA[] as Grouping[] for the constructor.

    Also, you don’t need the if (filtered != null) check, you will get a collection in filtered, it might just not produce any elements, but filtered will always be non-null.

    This means your code can be written as:

    var filtered = from r in lstGroupings.OfType<GroupingNNA>()
                   where r.QASensitivity == fSens
                   select r;
    return new List<Grouping>(filtered.ToArray());
    

    or even just:

    return new List<Grouping>((from r in lstGroupings.OfType<GroupingNNA>()
                               where r.QASensitivity == fSens
                               select r).ToArray());
    

    or even shorter if you drop the linq syntax:

    return new List<Grouping>((lstGroupings.OfType<GroupingNNA>()
        .Where(r => r.QASensitivity == fSens).ToArray());
    

    Note that you can of course use OfType to go the other way as well:

    return filtered.OfType<Grouping>().ToList();
    

    there shouldn’t be any big performance differences between the different ways here, if in doubt, measure it, but I would go with what you find easiest to read and understand.

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

Sidebar

Related Questions

I have a class that inherits from Page, called APage. public abstract class APage:
I have an abstract class called Node . It contains a constructor that takes
Ok so I have an abstract base class called Product, a KitItem class that
I have an abstract class, Foo, that has a non-abstract method called Bar. I
I have an abstract class: type TInterfaceMethod = class abstract public destructor Destroy; virtual;
I have one class called Person that basically looks like: public class Person {
I have an abstract base class called Curve . There are three classes that
Let's say that I have this: public abstract class myClass<T> : Ob<T> where T
Let's say I have an abstract parent class called shape, and that there are
For example, I have a class called Clothing that inherits from a abstract 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.