不能在元组列表上使用转换器
Can't use converter on list of tuples
首先,我的代码引用了System.ValueTuple
。
我有一个元组列表:
List<(string, string)> theme
并且我想在一次扫描中将元组的第一个字符串转换为 DateTime,因此我正在尝试创建一个转换器以与 List.ConvertAll
一起使用。
这不会给出错误:
var conv = new Converter<string,DateTime>(x => DateTime.ParseExact(x, "yyyy-MM-dd", null));
但显然这不是我需要的。当我尝试简单地将元组用作 lambda 的 input/output 时,出现错误:
(Delegate 'Converter)<(string,string),(DateTime,string)>' does not take two arguments)
var conv = new Converter<(string,string),(DateTime,string)>
( (x,y) => (DateTime.ParseExact(x, "yyyy-MM-dd", null),y) );
但我不会传递两个参数。还是我???
感谢您的帮助。
我认为这应该有效?
var conv = new Converter<(string, string), (DateTime, string)>(x => (DateTime.ParseExact(x.Item1, "yyyy-MM-dd", null), x.Item2));
您的第二次尝试非常接近。在以下语句中,(x,y)
表示将传递两个参数:
new Converter<(string,string),(DateTime,string)>((x,y) => (DateTime.ParseExact(x, "yyyy-MM-dd", null),y));
然而,它将收到的 Tuple
是单个参数,因此语句应该是:
new Converter<(string,string),(DateTime,string)>(x => (DateTime.ParseExact(x.Item1, "yyyy-MM-dd", null),x.Item2));
首先,我的代码引用了System.ValueTuple
。
我有一个元组列表:
List<(string, string)> theme
并且我想在一次扫描中将元组的第一个字符串转换为 DateTime,因此我正在尝试创建一个转换器以与 List.ConvertAll
一起使用。
这不会给出错误:
var conv = new Converter<string,DateTime>(x => DateTime.ParseExact(x, "yyyy-MM-dd", null));
但显然这不是我需要的。当我尝试简单地将元组用作 lambda 的 input/output 时,出现错误:
(Delegate 'Converter)<(string,string),(DateTime,string)>' does not take two arguments)
var conv = new Converter<(string,string),(DateTime,string)>
( (x,y) => (DateTime.ParseExact(x, "yyyy-MM-dd", null),y) );
但我不会传递两个参数。还是我??? 感谢您的帮助。
我认为这应该有效?
var conv = new Converter<(string, string), (DateTime, string)>(x => (DateTime.ParseExact(x.Item1, "yyyy-MM-dd", null), x.Item2));
您的第二次尝试非常接近。在以下语句中,(x,y)
表示将传递两个参数:
new Converter<(string,string),(DateTime,string)>((x,y) => (DateTime.ParseExact(x, "yyyy-MM-dd", null),y));
然而,它将收到的 Tuple
是单个参数,因此语句应该是:
new Converter<(string,string),(DateTime,string)>(x => (DateTime.ParseExact(x.Item1, "yyyy-MM-dd", null),x.Item2));