开发者

Tuple to Tuple conversion

开发者 https://www.devze.com 2023-02-20 19:38 出处:网络
Is there any easy way to convert Tuple<int, string, Guid> to Tuple<int, string>? UPDATE: ... to convert List<Tuple<int开发者_如何学运维, string, Guid>> to List<Tuple<int

Is there any easy way to convert Tuple<int, string, Guid> to Tuple<int, string>?

UPDATE: ... to convert List<Tuple<int开发者_如何学运维, string, Guid>> to List<Tuple<int, string>> ?


sure:

Tuple<int, string, Guid> t1 = ...;
Tuple<int, string> t2 = new Tuple<int, string>(t1.Item1, t1.Item2);

Update:
With the list:

List<Tuple<int, string, Guid>> t1s = ...;
List<Tuple<int, string>> t2s = t1s.Select(t1 => new Tuple<int, string>(t1.Item1, t1.Item2)).ToList();


For the updated question...

var result = tuples.Select(t => Tuple.Create(t.Item1, t.Item2)).ToList();

...should do the trick.

This takes the original list tuples and projects each 3-tuple t into the desired 2-tuple; the result of this is an IEnumerable<Tuple<int, string>> which may be sufficient if you can work with a lazy sequence, but as you've indicated you want a list then this is possible by appending a .ToList() call.


var myNewList = myList.Select(t => new Tuple<int, string>(t.Item1, t.Item2)).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消