C# 为什么lambda表达式采用char而不是string
我有以下使用lambda表达式的函数:C# 为什么lambda表达式采用char而不是string,c#,linq,lambda,C#,Linq,Lambda,我有以下使用lambda表达式的函数: Func<string, DateTime> GetDateFromFileName = fileName => Path.GetFileNameWithoutExtension(fileName) .Select(s => DateTime.ParseExact(s.Substring(s.Length - 8, 8), "yyyyMM
Func<string, DateTime> GetDateFromFileName
= fileName => Path.GetFileNameWithoutExtension(fileName)
.Select(s => DateTime.ParseExact(s.Substring(s.Length - 8, 8), "yyyyMMdd", null));
linq如何选择在lambda表达式中生成第一个变量的变量类型?
GetFileNameWithoutExtension
返回一个字符串,然后执行。选择。这意味着您将枚举字符串中的字符,因为字符串是字符数组
Linq将查看您正在使用的集合,并使用内部对象的类型
根据您的编辑,您可以将其更改为
Func<string, DateTime> GetDatesFromFileNames
= fileName =>
{
String filenamenoext = Path.GetFileNameWithoutExtension(fileName);
return DateTime.ParseExact(filenamenoext.Substring(filenamenoext.Length - 8, 8), "yyyyMMdd", null));
}
Func getdatesfromfilename
=文件名=>
{
字符串FileNameNoText=Path.GetFileNameWithoutExtension(文件名);
return DateTime.ParseExact(filenamenoext.Substring(filenamenoext.Length-8,8),“yyyyymmdd”,null));
}
GetFileNameWithoutExtension
返回一个字符串
,然后执行。选择打开。这意味着您将枚举字符串中的字符,因为字符串是字符数组
Linq将查看您正在使用的集合,并使用内部对象的类型
根据您的编辑,您可以将其更改为
Func<string, DateTime> GetDatesFromFileNames
= fileName =>
{
String filenamenoext = Path.GetFileNameWithoutExtension(fileName);
return DateTime.ParseExact(filenamenoext.Substring(filenamenoext.Length - 8, 8), "yyyyMMdd", null));
}
Func getdatesfromfilename
=文件名=>
{
字符串FileNameNoText=Path.GetFileNameWithoutExtension(文件名);
return DateTime.ParseExact(filenamenoext.Substring(filenamenoext.Length-8,8),“yyyyymmdd”,null));
}
好,因为字符串
是一个IEnumerable
,选择
将对字符串的字符
进行操作。也就是说,你也可以做:
foreach(var c in "hello") {
// c is a char
}
做你(可能)想做的事只需删除不必要的LINQ选择
:
Func<string, DateTime> GetDatesFromFileNames = fileName => {
var tmp = Path.GetFileNameWithoutExtension(fileName);
return DateTime.ParseExact(tmp.Substring(tmp.Length - 8, 8), "yyyyMMdd", null);
};
Func GetDatesFromFileNames=fileName=>{
var tmp=Path.GetFileNameWithoutExtension(文件名);
return DateTime.ParseExact(tmp.Substring(tmp.Length-8,8),“yyyyymmdd”,null);
};
将IEnumerable
作为返回值没有任何意义,因为文件名中很可能只有一个日期?好吧,因为string
是一个IEnumerable
,Select
将对字符串的char
进行操作。也就是说,你也可以做:
foreach(var c in "hello") {
// c is a char
}
做你(可能)想做的事只需删除不必要的LINQ选择
:
Func<string, DateTime> GetDatesFromFileNames = fileName => {
var tmp = Path.GetFileNameWithoutExtension(fileName);
return DateTime.ParseExact(tmp.Substring(tmp.Length - 8, 8), "yyyyMMdd", null);
};
Func GetDatesFromFileNames=fileName=>{
var tmp=Path.GetFileNameWithoutExtension(文件名);
return DateTime.ParseExact(tmp.Substring(tmp.Length-8,8),“yyyyymmdd”,null);
};
将IEnumerable
作为返回值是没有任何意义的,因为您的文件名中很可能只有一个日期?如果我读得正确,根据您的返回值Func
您有一些附有日期的文件列表:
fileA20140101
fileA20140102
fileB20140101
您需要得到一些包含以下内容的结果:
fileA: 01-01-2014
02-01-2014
fileB: 01-01-2014
但是,您的函数只接受一个文件名
,即使该名称包含文件名
。参数字符串文件名
是否需要改为IEnumerable fileNames
?查看函数的实现可能更有意义
根据评论进行更新
因此,您的函数只返回一个datetime
static DateTime GetDateFromFileName(string fileName) {
const string format = "yyyMMdd";
string withoutExtension = Path.GetFileNameWithoutExtension(fileName);
return DateTime.ParseExact(
withoutExtension.Substring(withoutExtension.Length - format.Length),
format, null);
}
Func<string, DateTime> myFunc = GetDateFromFileName;
静态日期时间GetDateFromFileName(字符串文件名){
常量字符串格式=“yyyMMdd”;
字符串withoutExtension=Path.getFileName withoutExtension(文件名);
return DateTime.ParseExact(
withoutExtension.Substring(withoutExtension.Length-format.Length),
格式,空);
}
Func myFunc=GetDateFromFileName;
如果我读对了,根据您的返回值Func
您有一些文件列表,其中附有日期:
fileA20140101
fileA20140102
fileB20140101
您需要得到一些包含以下内容的结果:
fileA: 01-01-2014
02-01-2014
fileB: 01-01-2014
但是,您的函数只接受一个文件名
,即使该名称包含文件名
。参数字符串文件名
是否需要改为IEnumerable fileNames
?查看函数的实现可能更有意义
根据评论进行更新
因此,您的函数只返回一个datetime
static DateTime GetDateFromFileName(string fileName) {
const string format = "yyyMMdd";
string withoutExtension = Path.GetFileNameWithoutExtension(fileName);
return DateTime.ParseExact(
withoutExtension.Substring(withoutExtension.Length - format.Length),
format, null);
}
Func<string, DateTime> myFunc = GetDateFromFileName;
静态日期时间GetDateFromFileName(字符串文件名){
常量字符串格式=“yyyMMdd”;
字符串withoutExtension=Path.getFileName withoutExtension(文件名);
return DateTime.ParseExact(
withoutExtension.Substring(withoutExtension.Length-format.Length),
格式,空);
}
Func myFunc=GetDateFromFileName;
好的,这很有道理。所以我可能不应该使用。然后选择。有没有一个更合适的选项可以让我处理整个字符串?@Dan我认为不使用linq会更容易些你想做什么?@DavidPilkington是的,很抱歉这是一个错误。我只想要一个文件名中的一个日期。我使用了一个多行Func
,我不知道这是否坏,但在我看来,它比在对象上使用一个无关的私有函数更干净。谢谢。@DavidPilkingtons
(在.Substring()
中)将不会被定义:/Ok,这很有意义。所以我可能不应该使用。然后选择。有没有一个更合适的选项可以让我处理整个字符串?@Dan我认为不使用linq会更容易些你想做什么?@DavidPilkington是的,很抱歉这是一个错误。我只想要一个文件名中的一个日期。我使用了一个多行Func
,我不知道这是否坏,但在我看来,它比在对象上使用一个无关的私有函数更干净。谢谢。@DavidPilkingtons
(在.Substring()
中)将不会被定义:/因此出现了另一个错误,它应该只返回一个日期时间。我的同位语,正如你早些时候所建议的,我试图重构它,使之只在一个方面工作