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

The Archive Base Latest Questions

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

How do you query a collection which is populated/created with select new ? I

  • 0

How do you query a collection which is populated/created with select new?

I have this BindingSource:

this.bindingSource.DataSource = 
    from row in db.Table
    select new 
    {
      name = row.Name + row.Num.ToString()
    };

I’d like to query it like I do with other BindingSources:

var query = from row in (IEnumerable<Table>)anotherBindingSource.List
            where row.name == "asd"
            select row;

Since bindingSource contains anonymous types I get this error:

Unable to cast object of type
‘System.Data.Linq.SortableBindingList1[<>f__AnonymousType815
etc. etc. to type ‘System.Collections.Generic.IEnumerable`1[Table]’.

What should I do?

  • 1 1 Answer
  • 1 View
  • 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-13T06:03:16+00:00Added an answer on May 13, 2026 at 6:03 am

    Well, not sure exactly what you’re trying to do here, but an anonymous type != a Table object. The exception indicates you’re trying to cast an IEnum of an anonymous type (a compiler-generated class with a weird name) to an IEnum of type Table.

    You can’t cast types willy nilly in C#. You can’t, for instance, do this:
    (Table)"Lol I'd like to be a table pls kthx"
    You can’t cast any type that isn’t already a Table, or extends from Table, to a Table.

    So what you’re asking is impossible. You should probably take a step back and ask a more general question about what you’re trying to accomplish.


    Some more on anon types… They only really have meaning within the scope of the method in which they are defined. It appears you might be returning your anon type enumeration from a method call and then are attempting to sort. This won’t work, as once the anonymous type leaves the method scope it is considered (at least by intellisense) to be an object and the only way to get at its properties is to use reflection.

    If your example isn’t just a simplified version, you could just skip the anon type altogether…

    this.bindingSource.DataSource = 
        from row in db.Table
        select row.Name + row.Num.ToString();
    

    This is an IEnumerable, and can be queried thusly:

    var query = from row in anotherBindingSource
                where row.StartsWith("asd")
                select row;
    

    However it doesn’t look like you’re accomplishing much at all with this…


    You cannot query anonymous types outside of the scope in which they are defined.

    This works:

    public void Worthless(Hurr hurr)
    {
      var query = from x in hurr select new { x.Durr };
    
      var requery = from x in query where x.Durr == "lol" select x;
    }
    

    This does not:

    public class Anonymous
    {
      public IEnumerable GetMyDurrs(Hurr hurr)
      {
        return from x in Hurr select new { x.Durr };
      }
    
      public IEnumerable WeedMyDurrs(Hurr hurr, string value)
      {
        // this won't compile
        return from x in GetMyDurrs(hurr) where x.Durr == value select x;
      }
    }
    

    The second example won’t compile because the anonymous type was defined within another scope.

    The only way to get this to work is to define a type.

    public class Anonymous
    {
      public IEnumerable<Anonymous.MyDurr> GetMyDurrs(Hurr hurr)
      {
        return from x in Hurr select new MyDurr { Durr = x.Durr };
      }
    
      public IEnumerable<Anonymous.MyDurr> WeedMyDurrs(Hurr hurr, string value)
      {
        // this won't compile
        return from x in GetMyDurrs(hurr) where x.Durr == value select x;
      }
    
      public class MyDurr { public string Durr {get;set;} }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 280k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer According to Scott Gu's blog, RC will be available sometime… May 13, 2026 at 3:32 pm
  • Editorial Team
    Editorial Team added an answer This is widely studied in intrusion detection literature. This is… May 13, 2026 at 3:32 pm
  • Editorial Team
    Editorial Team added an answer The problem was not special characters, but the '/' character… May 13, 2026 at 3:32 pm

Related Questions

I've always taken a data centric approach to web apps, and so the paradigm
I have a query sortable collection of images, ie. the items is set to
I'm currently working on a reasonably simple MVC app which allows the user to
I make a query: String query = SELECT DISTINCT a FROM A a FETCH
I have a java program that runs a bunch of queries against an sql

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.