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

  • Home
  • SEARCH
  • 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 955749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:26:03+00:00 2026-05-16T00:26:03+00:00

Had a problem in a complex linq query so I simplified it in LINQPad:

  • 0

Had a problem in a complex linq query so I simplified it in LINQPad:

void Main()
{
    List<basetype> items = new List<basetype>()
    {
        new typeA() { baseproperty = "1", extendedproperty = 1 },
        new typeB() { baseproperty = "2", extendedproperty = 1.1 },
        new typeA() { baseproperty = "3", extendedproperty = 1 },
    };

    items.Dump();

    (from typeA item in items
     where item is typeA
     select item).Dump();
}

public abstract class basetype
{
    public string baseproperty { get; set; }
    public string type { get; set; }
}

public class typeA : basetype
{
    public int extendedproperty { get; set; }
    public typeA() { type = "A"; }
}

public class typeB : basetype
{
    public double extendedproperty { get; set; }
    public typeB() { type = "B"; }
}

The first Dump works fine and returns:

extendedproperty baseproperty type 
1                1            A
1.1              2            B
1                3            A

However the second Dump errors with:

InInvalidCastException: Unable to cast object of type 'typeB' to type 'typeA'.

I can fix this by just removing the “typeA” but I wouldn’t want to do that in the original statement as I would have to cast the type all over the place:

from item in items

Interestingly enough, moving the where also fixes this though you might agree that’s a bit ugly:

from typeA item in items.Where(i => i is typeA)

My question is: why is the original where not filtering out the invalid item before the cast is evaluated?

  • 1 1 Answer
  • 2 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-16T00:26:03+00:00Added an answer on May 16, 2026 at 12:26 am

    Reason #1:

    The cast to the type happens before the filter because it comes to the left. In C# it is almost always the case that the thing to the left happens before the thing to the right.

    Reason #2:

    Suppose we did it your way. You have a List<object> and you say

    from string s in myObjects where s.Length > 100
    

    and you get an error saying that object doesn’t have a Length property – because of course with your way the cast to string happens after the filter, and therefore the filter cannot depend upon the invariant determined by the cast. Presumably you put the cast in there because you want to use the properties of the target type. But you can’t have it both ways; either the left operation runs first or the right operation runs first. They can’t both run first.

    Reason #3:

    There already is a way to do what you want:

    ... from foos.OfType<Bar>() ...
    

    That is equivalent to filtering first and then providing a sequence of just the filtered values of the right type.

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

Sidebar

Related Questions

No related questions found

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.