Search code examples
asp.netlinq-to-entitiesexpression-treesc#-7.0valuetuple

C# 7.0 Value Tuple compile error?


When I am trying to compile the following code:

var post = iPostService.GetAll().Select(x => (x.Title, x.Author));

I get the compiler error: 'An expression tree may not contain a tuple literal.'

So I also tried this:

var post = iPostService.GetAll().
                    Select(x => new ValueTuple<string, string>(x.Title, x.Author))

The result is runtime error:'Cannot resolve method Void .ctor(System.String, System.String) because the declaring type of the method handle System.ValueTuple`2[T1,T2] is generic. Explicitly provide the declaring type to GetMethodFromHandle.'

I googled to find out the solution to this problem but nothing really helpful.

Any help really appreciated.

Thanks


Solution

  • Finally, I found out what wrong with my code:

    • I am using deferred execution so data won't load from the database when the constructor executed.
    • The solution is to add conversion operators before creating instance command.

    Hope it works with your code.