C# 7.0 Value Tuple compile error?

asp.net c#-7.0 expression-trees linq-to-entities valuetuple

Question

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

Accepted Answer

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.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why