我最近在工作中遇到了一个问题,以下是源代码,
this.NameList = new ObservableCollection<Name>();
List<Others> others1 = new List<Others>();
others1.Add(new Others() { Company = "Company1", School = "School1" });
others1.Add(new Others() { Company = "Company1", School = "School1" });
others1.Add(new Others() { Company = "Company1", School = "School1" });
List<Others> others2 = new List<Others>();
others2.Add(new Others() { Company = "Company2", School = "School2" });
others2.Add(new Others() { Company = "Company2", School = "School2" });
others2.Add(new Others() { Company = "Company2", School = "School2" });
List<Others> others3 = new List<Others>();
others3.Add(new Others() { Company = "Company3", School = "School3" });
others3.Add(new Others() { Company = "Company3", School = "School3" });
others3.Add(new Others() { Company = "Company3", School = "School3" });
this.NameList.Add(new Name() { FirstName = "Jacob", LastName = "Deng", Others = others1 });
this.NameList.Add(new Name() { FirstName = "David", LastName = "Xu", Others = others2 });
this.NameList.Add(new Name() { FirstName = "Helen", LastName = "Liu", Others = others3 });
please click here to see the output of above code
然后请阅读以下代码,
List<Others> others1 = new List<Others>();
others1.Add(new Others() { Company = "Company1", School = "School1" });
others1.Add(new Others() { Company = "Company1", School = "School1" });
others1.Add(new Others() { Company = "Company1", School = "School1" });
List<Others> others2 = new List<Others>();
List<Others> others3 = new List<Others>();
this.NameList.Add(new Name() { FirstName = "Jacob", LastName = "Deng", Others = others1 });
this.NameList.Add(new Name() { FirstName = "David", LastName = "Xu", Others = others2 });
this.NameList.Add(new Name() { FirstName = "Helen", LastName = "Liu", Others = others3 });
Please click here for the output of the second snippet code
从上面的两个片段中,您可能会注意到第二个片段不包含other2和other3中的任何项目,您可以从预览输出中轻松理解。
现在问题来了,我们如何使用LINQ来IEnumerable来处理这种情况,如何使用LINQ从Others实体中删除那些项目?但我们需要保持other2和other3不为null(保持计数为0)。除了LINQ,还有其他解决方案吗?
我尝试使用以下内容,但失败了,
var others = ((MyVM)DataContext).NameList.Select(n => n.Others.Where(o => o.School == "School1")).ToList();
我刚做了一些测试,如果我们不使用LINQ,那么我们可以使用以下代码片段来修复它。
ObservableCollection<Name> nameList = ((MyVM)DataContext).NameList;
foreach(Name n in nameList)
{
List<Others> removeList = new List<Others>();
for(int i=0;i<n.Others.Count;i++)
{
if(n.Others[i].School!="School1")
{
removeList.Add(n.Others[i]);
}
}
foreach(Others other in removeList)
{
n.Others.Remove(other);
}
}
但它看起来非常冗余,我们知道LINQ非常有用,我相信它可以用LINQ修复。希望有人可以帮我修复LINQ,谢谢。
感谢有人可以帮助我。非常感谢。
答案 0 :(得分:2)
在您的案例中使用List<T>.RemoveAll会更好。有关详细信息,请参阅Using LINQ to remove elements from a List<T>。
ObservableCollection<Name> nameList = ((MyVM)DataContext).NameList;
foreach(Name n in nameList)
{
n.Others.RemoveAll(s => s.School != "School1");
}
Linq擅长只读迭代而不是更新/删除。如果你坚持使用Linq,你必须重新构建每个项目。
var newList = from nl in this.namelist
// this list can be empty but never null
let notSchool1 = (from s in nl.Others
where s.School != "School1"
select s).ToList()
select new Name
{
FirstName = nl.FirstName,
LastName = nl.LastName,
Others = noSchool1
};
this.NameList = new ObservableCollection<Name>(newList);
以上代码可能会破坏您应用中的其他功能,因为这些项目被观察。
答案 1 :(得分:0)
据我了解你的问题, 你想删除那些学校价值不高的学校1&#39;。有不同的方法,但我建议过滤你想要的NameList数据。
您可以尝试使用下面的代码来获取其他人列表&#39; School1&#39;
var result = NameList.Select(name => new Name()
{
FirstName = name.FirstName,
LastName = name.LastName,
Others = name.Others.Where(obj => obj.School == "School1").ToList()
}
).ToList();