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 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

Related Questions

I have a table which contains due dates for individual member records. Each row
I have a listview which loads its data from sqlite database. Each row in
This query works great: var pageObject = (from op in db.ObjectPermissions join pg in
This query is related to this one I asked yesterday. I have a radio
I have the following two table (which are tied in with Spring security -
I have a collection which contains two type of objects A & B. Class
This is for Entity Framework for .NET 3.5: I have the need to query
I have a LINQ query which results an array of Job records. Due to
I have used mongodb 1.8.1. In which I have collection which contains more than
The Query Optimizer is estimating that the results of a join will have only

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.