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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:08:07+00:00 2026-05-26T20:08:07+00:00

Is it possible to project using a Select onto a anonymous type? Here is

  • 0

Is it possible to project using a Select onto a anonymous type?

Here is some example code

public interface ITest
{
    string A{get;}
    int B{get;}
}

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IQueryable<ITest> query =
    from     n in names.AsQueryable()
    select   new {A = n.ToUpper(), B = 2012};

The above code causes a

Cannot implicitly convert type ‘System.Linq.IQueryable’ to ‘System.Linq.IQueryable’

Note: I can make the above code work if I were to define a class Test that implements ITest and then project into that class using:

select   new Test {A = n.ToUpper(), B = 2012};

Why? I am trying to see if I can only define the interface and not have to define the concrete implementation of the object and have Linq create me the object.

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

    Yes, you can select into an anonymous type, but you can’t both select into an anonymous type and then use those objects as if they implemented an interface. The reason is that the anonymous type doesn’t implement the interface even though they have the same properties, in both type and name. There isn’t a way to define that an anonymous type implements an interface. If you want to use objects via their interface implementations, you have to select into a concrete type that implements that interface.

    Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object. The compiler provides a name for each anonymous type, although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type.

    Ref: http://msdn.microsoft.com/en-us/library/bb397696.aspx

    I can certainly sympathize with your intent here. It would be nice if the compiler could intuit that the type meets the interface definition when the type is anonymous, but that’s really only applicable when the interface consists strictly of read-only properties. The minute your interface defines properties with setters or methods, it’s not possible for an anonymous type to implement it. On the other hand, if you’re using the anonymous type as it’s intended — as a short-term temporary type for a particular use — you can simply reference it’s properties and you don’t need the interface at all.

     var query = from     n in names.AsQueryable()
                 select   new {A = n.ToUpper(), B = 2012};
     foreach (var item in query)
     {
          Console.WriteLine( item.A );
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want make datetimepicker in my project. Using jquery how it is possible? I
Doing some refactoring in some legacy code I've found in a project. This is
i'm making a JSF2.0 project using mojarra primefaces tomcat6.x. I made a select list
I am using jquery to add a few features onto some fields in the
I have a project using Entity Framework code first. For various reasons one of
It seems to be possible in this project ... After they use pd =
Possible Duplicate: VS2008 Setup Project: Shared (By All Users) Application Data Files? Please can
Is that possible to switch an Open Source Project license from GPL to LGPL
Is it possible to verify which source safe project is bound to my vs.net
Possible stupid question: let's say I have two apps contained within one project. Can

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.