Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

.net - Is there a shorthand way to convert a Tuple to a ValueTuple?

I have just included a DLL file into my project and am using a method which returns a List<Tuple>. I have only recently became aware of the ValueTuple datatype in C# 7 and want to use it instead of the normal Tuples which are returned. I am aware that I could loop through and do the conversion manually however I would like to avoid doing this if possible and was wondering if anyone was aware of a shorthand way to casting a Tuple to a ValueTuple? If I am being ignorant and this isn't possible then I would be grateful if someone could make me aware of the most performant way I could achieve this conversion.

I have looked for a .ToValueTuple() method and using a basic cast on the function returning the List with no joy.

When attempting:

List<(string entityName, int min, int average, int max)> tupTest = trs.GetEntityStats();

I receive the error:

Cannot convert source type 'System.Collections.Generic.List>' to target type 'System.Collections.Generic.List<(string entityName, int min, int average, int max)>'

question from:https://stackoverflow.com/questions/65952783/convert-from-ienumerabletuplet1-t2-to-ienumerablet1-t2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The TupleExtensions Class has ToTuple and ToValueTuple methods.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...