Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 522
Tan Sang Avatar asked Jan 07 '18 14:01

Tan Sang


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

What language is C in?

C is a procedural language that provides no support for objects and classes. C++ is a combination of OOP and procedural programming languages. C has 32 keywords and C++ has 63 keywords. C supports built-in data types, while C++ supports both built-in and user-defined data types.


1 Answers

It works for me, create a tuple first and convert it to a ValueTuple:

var post = iPostService.GetAll().
           Select(x => new Tuple<string, string>(x.Title, x.Author).ToValueTuple())
like image 81
Steven Chou Avatar answered Sep 19 '22 14:09

Steven Chou