Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning property of anonymous type via anonymous method

I am new in the functional side of C#, sorry if the question is lame.

Given the following WRONG code:

var jobSummaries = from job in jobs
                   where ...
                   select new 
                   {
                        ID = job.ID,
                        Description = job.Description,
                        FileName = (job) => {
                                  // primitive logic not 
                                  // worth to become a named method
                                  try { return job.Files[0].LocalName); }
                                  catch { return null as string; }
                                 }
                   };

This code produces the following justified compiler error:

cannot assign lambda expression to anonymous type property

The code above would set the delegate to the FileName property. But that is not my aim. I want the code work like this but without naming the method:

var jobSummaries = from job in jobs
                   where ...
                   select new 
                   {
                        ID = job.ID,
                        Description = job.Description,
                        FileName = this.ExtractFileName(job)
                   };

...
private string ExtractFileName(Job job)
{
     try { return Path.GetFileName(job.Files[0].LocalName); }
     catch { return null as string; }
}

Any suggestions?

like image 352
GarbageGuy Avatar asked Mar 03 '10 13:03

GarbageGuy


People also ask

Which of the following selects an anonymous type?

In LINQ, select clause generates anonymous type so that in a query you can include properties that are not defined in the class.

How do you define anonymous type?

Essentially an anonymous type is a reference type and can be defined using the var keyword. You can have one or more properties in an anonymous type but all of them are read-only. In contrast to a C# class, an anonymous type cannot have a field or a method — it can only have properties.

What is the use of anonymous method in C#?

An anonymous method is a method which doesn't contain any name which is introduced in C# 2.0. It is useful when the user wants to create an inline method and also wants to pass parameter in the anonymous method like other methods.

What are the limitations of the anonymous method?

Limitations of Anonymous MethodsAny unsafe codes cannot be accessed inside anonymous methods. There cannot be jump statements such as goto, break or continue inside anonymous methods. Anonymous methods cannot be used on the left side of the is operator.


2 Answers

To call an anonymous function directly, this works:

int result = new Func<int, int>( (int i) =>{ return i + 5; } ).Invoke(3);
// result = 8

But I agree, int result = (i => i + 5)(3); would be way cooler =)

like image 134
Jens Avatar answered Sep 18 '22 13:09

Jens


As far as I know, you can't inline lambda expressions like that because a lamda expression is an instance itself (of the type Expression<Func<T>> or similar).

However, you can do this (updated with calculation of fileName, since this is now provided by the OP):

var jobSummaries = from job in jobs
                   where ...
                   let fileName = job.Files.Select(f => f.LocalName).FirstOrDefault()
                   select new 
                   {
                        ID = job.ID,
                        Description = job.Description,
                        FileName = fileName
                   };

Notice the use of the let keyword, that lets you extract the filename from the job variable directly inside the LINQ expression.

like image 34
Mark Seemann Avatar answered Sep 17 '22 13:09

Mark Seemann