0
- 我有一個文件名列表(不是完整的路徑)。 (IEnumerable < string>)
- 我有一個類的列表,其中包含一個字符串屬性(FullPath),其中包含完整的文件路徑。 (列表< CustomClass>)
如果任何CustomClass FullPath屬性存在於文件名列表中,它應該被刪除/忽略。我該如何改進這個C#.NET4代碼?
到目前爲止,我有這樣的:
this.myClasses.Add(new MyClass() { FullPath = @"C:\WorkingDirectory\File1.txt" });
files = new List<string>(new string[] { "File1.txt", "File2.txt", "File3.txt"})
List<string> newFiles = (from file in files
where File.Exists(Path.GetFullPath(file))
select Path.GetFullPath(file))
.ToList();
newFiles.RemoveAll(delegate(string path)
{
var r = from myClass in this.myClasses
where myClass.FullPath == path
select myClass;
return r.Count() > 0;
});
newFiles現在包含文件2和文件3的完整路徑。
是的,他們必須都存在,我想要完整的路徑。所以你的例子no 4可能是我想要的方式。 –
@CS:是的,例4是你想要的。 –