我有一个项目列表让我们说| 1 | 2 | 3 | 4 | 5 |我想取1并将其与列表的其余部分进行比较,但我不希望1与自身进行比较。在1与列表的其余部分相比之后,我认为想要将1回到列表中,然后取出2并与此次列表的其余部分进行比较,包括1.这是我尝试过的。这里的问题是当我第一次迭代它完美时,因为第一次迭代从0开始并比较从1开始,但是一旦它循环超过0索引,它将开始比较它自己导致问题。任何帮助都是极好的。我知道这只是一个逻辑问题,我必须弄清楚,但我有点失落。
这是我试过的
if (RadUpload1.UploadedFiles.Count > 1)
{
for (int fileBuffer = 0; fileBuffer < RadUpload1.UploadedFiles.Count; fileBuffer++)
{
for (int fileList = 1; fileList < RadUpload1.UploadedFiles.Count; fileList++)
{
if (RadUpload1.UploadedFiles[fileBuffer] != RadUpload1.UploadedFiles[fileList])
{
if (RadUpload1.UploadedFiles[fileBuffer].GetName() == RadUpload1.UploadedFiles[fileList].GetName())
{
lbl_message.Text = "There where duplicates files found please check the files and try again";
}
else
{
//do other stuffs
}
答案 0 :(得分:1)
if (RadUpload1.UploadedFiles.Count > 1)
{
for (int fileBuffer = 0; fileBuffer < RadUpload1.UploadedFiles.Count-1; fileBuffer++)
{
for (int fileList = fileBuffer + 1; fileList < RadUpload1.UploadedFiles.Count; fileList++)
{
if (RadUpload1.UploadedFiles[fileBuffer] != RadUpload1.UploadedFiles[fileList])
{
//....
答案 1 :(得分:1)
所以你想找到重复的东西? LINQ会极大地简化它:
var list = new List<int> { 1, 2, 3, 4, 5 };
var dups = list.GroupBy(i => i).Where(g => g.Count() > 1);
foreach (var dup in dups)
{
int i = dup.Key;
int count = dup.Count();
}
这也适用于您的UploadedFileCollection
(在Cast
的帮助下,因为它只实现了非通用的CollectionBase
类):
var dupFiles = uploadedFiles.Cast<UploadedFile>()
.GroupBy(f => f.GetName())
.Where(g => g.Count() > 1);
foreach (var dupFile in dupFiles)
{
string fileName = dupFile.Key;
int count = dupFile.Count();
}
答案 2 :(得分:1)
我知道这不是使用你的代码,而是将每个项目与每个项目进行比较的简单概念,但它是自己的,祝你好运。
def search(alist):
for anitem in alist:
for anotheritem in alist:
if alist.index(anitem) == alist.index(anotheritem):
pass
else:
Do something
答案 3 :(得分:1)
要从列表中删除和添加项目,您可以使用Object.ReferenceEquals来了解它是否是同一个File实例:
试试这段代码:
class Program
{
static void Main(string[] args)
{
List<File> FileList = new List<File>();
FileList.Add(new File { FullPath = "File1" });
FileList.Add(new File { FullPath = "File2" });
FileList.Add(new File { FullPath = "File3" });
FileList.Add(new File { FullPath = "File4" });
FileList.Add(new File { FullPath = "File5" });
//FileList.Add(new FileName { FullPath = "File5" });
foreach (File SourceFile in FileList)
{
foreach (File TestFile in FileList)
{
if (SourceFile.GetName() == TestFile.GetName() && !(Object.ReferenceEquals(SourceFile, TestFile)))
{
var lbl_message = "There where duplicates files found please check the files and try again";
}
else
{
//another stuff
}
}
}
}
}
public class File
{
public string FullPath;
public string GetName()
{
return FullPath;
}
}
答案 4 :(得分:0)
你也可以使用linq查询:
var duplicatesFiles = from file in FileList
group file by file.GetName() into groupedbyname
where groupedbyname.Count() > 1
select groupedbyname.Count();
bool thereAreDuplicates = duplicatesFiles.Count() > 0;