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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:58:23+00:00 2026-06-03T11:58:23+00:00

The code below is valid: IEnumerable<SomeThing> things = …; // map type SomeThing to

  • 0

The code below is valid:

IEnumerable<SomeThing> things = ...;

// map type SomeThing to a new anonymous type, resulting in a strongly typed 
// sequence based on an anon type

var newList = things.Select(item =>
    {
        return new
        {
            ID = item.ID,
            DateUpdatedOrCreated = ((DateTime)(item.DateUpdated ??
                     item.DateCreated)).ToShortDateString(),
            Total = item.Part1 + item.Part2
        };
    });

newList now appears in Visual Studio as IEnumerable<'a> and is strongly typed with the anonymous type created in the function. That is so cool.

What I can’t seem to do is figure out a way to assign just the lambda expression (and not the enumeration) to an implicitly typed variable. Even though the compiler has no problem with the anonymous type in the context above, if I try (say)

var func = (SomeThing item)=> {
        return new { ... };
    };

I get the error “Cannot assign lambda expression to implicitly-typed local variable”. This seems a strange compiler limitation; unless I am missing something, the types are just as non-ambiguous in the 2nd example as they are in the first first: both type parameters are well defined.

Is there any way to do this? Since it’s an anonymous type, of course, I don’t have any way to use a type to assign it explicitly, so it seems I’d be stuck with making a class for the output type if not.

Update

Shortly after going about my merry way with Jon Skeet’s answer, I found a similar dilemma instantiating classes. In case it’s not obvious, the same trick can be used to create strongly typed classes using inferred anonymous types.

class Processor<T,U>
{
    public Processor(Func<T,U> func) {

    }
}

// func is a delegate with anon return type created using method in answer below

var instance = new Processor(func);   // does not compile! Requires type arguments!

cannot be created directly, but can be created in much the same way as the trick below:

public static Processor<T,U> Create<T,U>(Func<T,U> func) {
    return new Processor<T,U>(func);
}

var instance = Processor.Create(func);   // all good
  • 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-06-03T11:58:28+00:00Added an answer on June 3, 2026 at 11:58 am

    You can do it via type inference:

    var func = BuildFunc((SomeThing item) => {
        return new { ... };
    });
    
    ...
    
    static Func<TSource, TResult> BuildFunc<TSource, TResult>(
        Func<TSource, TResult> function) {
        return function;
    }
    

    Note that BuildFunc doesn’t really do anything – it just provides the method call needed to get the compiler to do type inference for the generic type arguments for Func<,> – it adds the information that you’re interested in Func<,>, basically – that’s information which can’t be specified as part of a variable declaration, without also specifying the type arguments.

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

Sidebar

Related Questions

Is something like the code below valid? struct foo { int a; int b[];
Is the code below valid XML? <sometag attribute1=value1 <!-- attribute2=value2 --> attribute3=value3> </sometag>
Is this a valid function pointer code below, In views , def change(request): dict={}
Considering the code below: Dataview someView = new DataView(sometable) someView.RowFilter = someFilter; if(someView.count >
The code below kinda works except yes.com is shown as valid while I need
Hi I have created the code below to check if a user is valid
Nothing shows on the screen, is this valid code below? I know theres a
i am wondering if the below code is valid as there is no method
Consider the code below. Looks like perfectly valid C# code right? //Project B using
When I try the code below in Cassini, I get a valid response. When

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.