C# 如何检查是否尝试通过LINQ复制旧文件
我想得到答案,如果我可以替换旧文件或不-如果文件是旧的,我不想复制。问题是-每个目录中的文件数可能不同 我不想写很多方法,只想写一个简单的LINQ查询,但我在LINQ方面相当弱 我想使用LINQ返回true或falseC# 如何检查是否尝试通过LINQ复制旧文件,c#,linq,C#,Linq,我想得到答案,如果我可以替换旧文件或不-如果文件是旧的,我不想复制。问题是-每个目录中的文件数可能不同 我不想写很多方法,只想写一个简单的LINQ查询,但我在LINQ方面相当弱 我想使用LINQ返回true或false System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(sources).GetFiles(); System.IO.FileInfo[] filesTarget = new System.IO.Direc
System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(sources).GetFiles();
System.IO.FileInfo[] filesTarget = new System.IO.DirectoryInfo(target).GetFiles();
bool canCopy = ...
/* group - if file have the same name
if can't match and group - simply ignore it */ ...
(x => x.Source.LastWriteTime < x.Target.LastWriteTime).Count() == 0;
您的问题不是很清楚,但这会复制源中较新的文件
foreach (var target in from target in filesTarget from source in filesSource where target.LastWriteTime < source.LastWriteTime select target)
{
// Copy
}
您的问题不是很清楚,但这会复制源中较新的文件
foreach (var target in from target in filesTarget from source in filesSource where target.LastWriteTime < source.LastWriteTime select target)
{
// Copy
}
这是我能想到的最好的了 林奇道:
System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(source).GetFiles();
System.IO.FileInfo[] filesTarget = new System.IO.DirectoryInfo(dest).GetFiles();
bool canCopy = !(from fileInfo in filesSource
let tmp = filesTarget.FirstOrDefault(f => f.Name == fileInfo.Name)
where tmp != null && tmp.LastWriteTime > fileInfo.LastWriteTime
select fileInfo).Any();
正常方式:
private static bool CanCopyAllFiles(string source, string dest)
{
System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(source).GetFiles();
System.IO.FileInfo[] filesTarget = new System.IO.DirectoryInfo(dest).GetFiles();
foreach (FileInfo fileInfo in filesSource)
{
FileInfo tmp = filesTarget.FirstOrDefault(f => f.Name == fileInfo.Name);
if (tmp != null && tmp.LastWriteTime > fileInfo.LastWriteTime)
{
return false;
}
}
return true;
}
这是我能想到的最好的了 林奇道:
System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(source).GetFiles();
System.IO.FileInfo[] filesTarget = new System.IO.DirectoryInfo(dest).GetFiles();
bool canCopy = !(from fileInfo in filesSource
let tmp = filesTarget.FirstOrDefault(f => f.Name == fileInfo.Name)
where tmp != null && tmp.LastWriteTime > fileInfo.LastWriteTime
select fileInfo).Any();
正常方式:
private static bool CanCopyAllFiles(string source, string dest)
{
System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(source).GetFiles();
System.IO.FileInfo[] filesTarget = new System.IO.DirectoryInfo(dest).GetFiles();
foreach (FileInfo fileInfo in filesSource)
{
FileInfo tmp = filesTarget.FirstOrDefault(f => f.Name == fileInfo.Name);
if (tmp != null && tmp.LastWriteTime > fileInfo.LastWriteTime)
{
return false;
}
}
return true;
}
如果您希望在linq中执行此操作,您可以加入名称上的列表,然后比较源和目标之间的LastWriteTime
var filesToCopy = filesSource
.GroupJoin(filesTarget, source => source.Name, target => target.Name,
(source, target) => new { Source = source, Target = target })
.Where(x => !x.Target.Any() || x.Target.First().LastWriteTime < x.Source.LastWriteTime);
foreach (var file in filesToCopy)
{
// copy file
}
如果您希望在linq中执行此操作,您可以加入名称上的列表,然后比较源和目标之间的LastWriteTime
var filesToCopy = filesSource
.GroupJoin(filesTarget, source => source.Name, target => target.Name,
(source, target) => new { Source = source, Target = target })
.Where(x => !x.Target.Any() || x.Target.First().LastWriteTime < x.Source.LastWriteTime);
foreach (var file in filesToCopy)
{
// copy file
}
因此,您需要做的第一件事是连接两个文件集合。您需要将每个源文件与其目标文件(如果有)进行匹配。这是通过GroupJoin完成的,而不是常规的join,因为我们希望返回没有匹配目标项的项 一旦我们完成了连接,我们就可以过滤出既有目标文件又有目标文件较新的项目
public static IEnumerable<FileInfo> FilesToCopy(DirectoryInfo source, DirectoryInfo target)
{
return from sourceFile in source.GetFiles()
join targetFile in target.GetFiles()
on sourceFile.FullName equals targetFile.FullName
into destinationFiles
let targetFile = destinationFiles.FirstOrDefault()
where !destinationFiles.Any() ||
sourceFile.LastWriteTime > targetFile.LastWriteTime
select sourceFile;
}
因此,您需要做的第一件事是连接两个文件集合。您需要将每个源文件与其目标文件(如果有)进行匹配。这是通过GroupJoin完成的,而不是常规的join,因为我们希望返回没有匹配目标项的项 一旦我们完成了连接,我们就可以过滤出既有目标文件又有目标文件较新的项目
public static IEnumerable<FileInfo> FilesToCopy(DirectoryInfo source, DirectoryInfo target)
{
return from sourceFile in source.GetFiles()
join targetFile in target.GetFiles()
on sourceFile.FullName equals targetFile.FullName
into destinationFiles
let targetFile = destinationFiles.FirstOrDefault()
where !destinationFiles.Any() ||
sourceFile.LastWriteTime > targetFile.LastWriteTime
select sourceFile;
}
你不能指望Linq完成所有的工作,写几行代码不会有什么坏处!FileSource中的每个文件可能有也可能没有FileTargeti中的现有文件我只需将具有相同名称的文件分组,一切都会很好。我们可以在两个目录中使用相同的文件名,但使用不同的WriteTime,您不能期望Linq完成所有工作,编写几行代码不会有什么坏处!FileSource中的每个文件可能有也可能没有FileTargeti中的现有文件我只需将具有相同名称的文件分组,一切都会很好。我们可以在两个目录中使用相同的文件名,但使用不同的WriteTimes。您需要在where子句中添加目标文件名和源文件名的比较。您需要在where子句中添加目标文件名和源文件名的比较。请注意,这不会复制源目录中但不包含的文件在目标目录中,可能也应该复制。请注意,这不会复制源目录中但不在目标目录中的文件,目标目录可能也应该复制。