Async lambda to Expression<Func<Task>>

ForNeVeR

It is widely known that I can convert ordinary lambda expression to Expression<T>:

Func<int> foo1 = () => 0; // delegate compiles fine
Expression<Func<int>> foo2 = () => 0; // expression compiles fine

How could I do the same with async lambda? I've tried the following analogy:

Func<Task<int>> bar1 = async () => 0; // also compiles (async lambda example)
Expression<Func<Task<int>>> bar2 = async () => 0; // CS1989: Async lambda expressions cannot be converted to expression trees

Is there any workaround possible?

Akash Kava

C# can only convert lambda expression to Expression tree only if code can be represented by Expression Tree, if you notice, there is no equivalent of "async" keyword in Expressions in System.Linq.Expressions

So not only async, but anything in C# that has no equivalent expression in provided Expressions, C# can't convert it to Expression Tree.

Other examples are

  1. lock
  2. unsafe
  3. using
  4. yield
  5. await

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Async lambda to Expression<Func<Task>>

From Dev

Lambda expression to Func<T, TResult>

From Dev

delegate and lambda expression task

From Dev

async lambda expression with arguments

From Dev

Replace lambda expression in MongoDB query with Func

From Dev

how to Convert Lambda expression to Func<> variable

From Dev

using Extension Method or Func in EF lambda expression

From Dev

Lambda expression not correct - need to return a boolean Func

From Dev

Lambda works in FindAll, but not when using it as an Func (or Expression)

From Dev

Func<T> in Lambda expression with LinQ to entities

From Dev

Cast lambda expression to Func<IObserver<char>, IDisposable>

From Dev

Negating Func<T, bool> in lambda expression

From Dev

Discrete Sum of Func(T, TResult) with lambda expression

From Dev

Func<T> in Lambda expression with LinQ to entities

From Dev

Expression in With statement becomes Nothing in lambda expression in Task

From Dev

Update UI from async task lambda in WinRT

From Dev

is using an an `async` lambda with `Task.Run()` redundant?

From Dev

Expression<Func< - Operator '||' cannot be applied to operands of type 'lambda expression' and 'lambda expression'

From Dev

Getting value from Task with lambda expression

From Dev

Casting/converting an expression back to a lambda of Func<Foo,bool> (after serialization)

From Dev

Func<t, bool> vs Manually expression performance in C# lambda

From Dev

Converting the result of Func<T, IConvertible> to int in a lambda expression

From Dev

Wait for finish all async methods inside an Async lambda expression

From Dev

Task.Run Exception with non-async lambda

From Dev

The 'await' operator can only be used within an async lambda expression error

From Dev

The 'await' operator can only be used with an async lambda expression

From Dev

The 'await' operator can only be used within an async lambda expression

From Dev

The 'await' operator can only be used within an async lambda expression error

From Dev

Explicitly use a Func<Task> for asynchronous lambda function when Action overload is available

Related Related

  1. 1

    Async lambda to Expression<Func<Task>>

  2. 2

    Lambda expression to Func<T, TResult>

  3. 3

    delegate and lambda expression task

  4. 4

    async lambda expression with arguments

  5. 5

    Replace lambda expression in MongoDB query with Func

  6. 6

    how to Convert Lambda expression to Func<> variable

  7. 7

    using Extension Method or Func in EF lambda expression

  8. 8

    Lambda expression not correct - need to return a boolean Func

  9. 9

    Lambda works in FindAll, but not when using it as an Func (or Expression)

  10. 10

    Func<T> in Lambda expression with LinQ to entities

  11. 11

    Cast lambda expression to Func<IObserver<char>, IDisposable>

  12. 12

    Negating Func<T, bool> in lambda expression

  13. 13

    Discrete Sum of Func(T, TResult) with lambda expression

  14. 14

    Func<T> in Lambda expression with LinQ to entities

  15. 15

    Expression in With statement becomes Nothing in lambda expression in Task

  16. 16

    Update UI from async task lambda in WinRT

  17. 17

    is using an an `async` lambda with `Task.Run()` redundant?

  18. 18

    Expression<Func< - Operator '||' cannot be applied to operands of type 'lambda expression' and 'lambda expression'

  19. 19

    Getting value from Task with lambda expression

  20. 20

    Casting/converting an expression back to a lambda of Func<Foo,bool> (after serialization)

  21. 21

    Func<t, bool> vs Manually expression performance in C# lambda

  22. 22

    Converting the result of Func<T, IConvertible> to int in a lambda expression

  23. 23

    Wait for finish all async methods inside an Async lambda expression

  24. 24

    Task.Run Exception with non-async lambda

  25. 25

    The 'await' operator can only be used within an async lambda expression error

  26. 26

    The 'await' operator can only be used with an async lambda expression

  27. 27

    The 'await' operator can only be used within an async lambda expression

  28. 28

    The 'await' operator can only be used within an async lambda expression error

  29. 29

    Explicitly use a Func<Task> for asynchronous lambda function when Action overload is available

HotTag

Archive