我会尽力解释我遇到的情况。基本上我有两个清单。
现在我要做的是选择其他列表中不存在的所有值。但问题是,如果我使用$searchpath = "e:\"
# Find a unique list of directories that contains a revit backup file (*.*.rvt)
$a = Get-ChildItem -Path $searchpath -Include *.*.rvt -Recurse | Select Directory | Get-Unique -AsString
# For each folder that contains a single revit backup file (*.*.rvt)...
# - Sort by modified time
# - Select all except first 5
$a | foreach {
$b += Get-ChildItem -Path $_.Directory.FullName -Include *.*.rvt | Sort-Object LastWriteTime -descending | select-object -skip 5 -property Directory,Name,CreationTime,LastWriteTime
}
$b | Out-GridView -Title "Old Backups" -PassThru
扩展方法并选择两个属性,则它总是返回0值。
这是我的代码:
第一个列表:
All
第二个清单:
IList<Person> lstOne = new List<Person>();
lstOne.Add(new Person()
{
ID = 1,
IDSecond = 2,
IDThird = 2
});
lstOne.Add(new Person()
{
ID = 2,
IDSecond = 3,
IDThird = 2
});
现在,如果我想选择第一个列表中不存在的第二个列表,我通常会这样做:
IList<Person> lstFinal = new List<Person>();
lstFinal.Add(new Person()
{
ID = 1,
IDSecond = 2,
IDThird = 2
});
lstFinal.Add(new Person()
{
ID = 3,
IDSecond = 4,
IDThird = 2
});
lstFinal.Add(new Person()
{
ID = 4,
IDSecond = 5,
IDThird = 2
});
这很好用,结果将是两个值:
var resultOne = lstFinal.Where(c => lstOne
.All(x => x.ID != c.ID && x.IDSecond != c.IDSecond)).ToList();
如果我选择一个或两个属性,它可以正常工作。但是如果我在项目中添加了我需要的其他属性,结果总是为零:
ID: 3 and ID: 4
我的问题应该是这段代码:
var resultOne = lstFinal.Where(c => lstOne
.All(x => x.ID != c.ID && x.IDSecond != c.IDSecond && x.IDThird != c.IDThird)).ToList();
返回两个值而不是没有?我期待它返回它,但它返回0值:
var resultOne = lstFinal.Where(c => lstOne
.All(x => x.ID != c.ID && x.IDSecond != c.IDSecond && x.IDThird != c.IDThird)).ToList();
答案 0 :(得分:0)
Enumerable已经定义了一些为您执行此操作的功能,因此您不必重新发明轮子
选中此MSDN
class Compare : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if (x == null || y == null)
{
return false;
}
return x.ID == y.ID && x.IDSecond == y.IDSecond;
}
public int GetHashCode(Person obj)
{
return 1;
}
}
class FullCompare : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if (x == null || y == null)
{
return false;
}
return x.ID == y.ID && x.IDSecond == y.IDSecond && x.IDThird == y.IDThird;
}
public int GetHashCode(Person obj)
{
return 1;
}
}
class Person
{
public int ID { get; set; }
public int IDSecond { get; set; }
public int IDThird { get; set; }
}
class Program
{
static void Main(string[] args)
{
IList<Person> lstOne = new List<Person>();
lstOne.Add(new Person()
{
ID = 1,
IDSecond = 2,
IDThird = 2
});
lstOne.Add(new Person()
{
ID = 2,
IDSecond = 3,
IDThird = 2
});
IList<Person> lstFinal = new List<Person>();
lstFinal.Add(new Person()
{
ID = 1,
IDSecond = 2,
IDThird = 2
});
lstFinal.Add(new Person()
{
ID = 3,
IDSecond = 4,
IDThird = 2
});
lstFinal.Add(new Person()
{
ID = 4,
IDSecond = 5,
IDThird = 2
});
var a = lstFinal.Except(lstOne, new Compare());
var b = lstFinal.Except(lstOne, new FullCompare());
a.ToList().ForEach(s => Console.WriteLine(s.ID));
b.ToList().ForEach(s => Console.WriteLine(s.ID));
Console.ReadLine();
//Console.WriteLine();
}
}