从我正在迭代的列表中删除项目,或过滤复杂的重复列表
Remove item from List that I'm iterating, or filtering complex List of duplicates
好吧,也许我只是瞎了眼,但我没有找到答案:
所以,型号:
public class Dimensions
{
public int Width { get; set; }
public int Height { get; set; }
public int TypeId { get; set; }
public int PeacesForItem { get; set; }
}
我有一个过滤列表的方法:
public List<Dimensions> Consolidation(List<Dimensions> vm)
{
var list = new List<Dimensions>();
if (vm != null)
{
var typeIds = vm.Select(x => x.TypeId).ToHashSet();
foreach (var t in typeIds)
{
foreach(var item in vm)
{
if (t == item.IvericaId)
{
int x = 0;
foreach (var again in vm)
{
if (item.Duzina == again.Duzina && item.Sirina == again.Sirina && item.TypeId== again.TypeId)
{
x ++;
// vm.Remove(item); Not working, who would figure
}
}
list.Add(
new Dimensions
{
Width = item.Width,
Height = item.Height,
TypeId= item.TypeId,
PeacesForItem = x * item.PeacesForItem,
}
);
}
}
}
}
return list;
}
此方法遍历列表项并检查是否存在相同维度的元素。如果有,那就是需要的数量翻倍。
问题:此代码仍在向新列表中添加重复项,我需要将其过滤掉。
我尝试了很多方法,但我想出的每一个在设计上都有一些致命的缺陷。
复制 vm-List 并迭代复制的列表并从原始列表中删除对象
public List<Dimensions> Consolidation(List<Dimensions> vm)
{
var list = new List<Dimensions>();
if (vm != null)
{
var dublicate = new List<Dimensions>(vm);
var typeIds = vm.Select(x => x.TypeId).ToHashSet();
foreach (var t in typeIds)
{
foreach(var item in dublicate )
{
if (t == item.IvericaId)
{
int x = 0;
foreach (var again in dublicate )
{
if (item.Duzina == again.Duzina && item.Sirina == again.Sirina && item.TypeId== again.TypeId)
{
x ++;
vm.Remove(item); //Now working
}
}
list.Add(
new Dimensions
{
Width = item.Width,
Height = item.Height,
TypeId= item.TypeId,
PeacesForItem = x * item.PeacesForItem,
}
);
}
}
}
}
return list;
}
public List<Dimensions> Consolidation(List<Dimensions> vm)
{
return vm.GroupBy(d=>new {d.TypeId, d.Width, d.Height}) // if there are any duplicates, they are grouped here
.Select(g=>new Dimensions(){TypeId = g.Key.TypeId ,
Width = g.Key.Width,
Height = g.Key.Height,
PeacesForItem = g.Sum(dim=>dim.PeacesForItem)}) // number of duplicates in group calculated
.ToList();
}
你把这个问题复杂化了 :)。您不需要嵌套循环,而且您不是将值加倍,而是 平方 吗?试试这个:
var list = new List<Dimensions>();
if (vm != null)
{
foreach (var item in vm)
{
//Not sure how you identify dupes, may need to change the filter here
var duplicate = vm
.Where(v => v.PeacesForItem == item.PeacesForItem);
if (dupes.Any())
{
list.Add(new Dimension
{
Width = item.Width,
Height = item.Height,
TypeId = item.TypeId,
PeacesForItem = item.PeacesForItem * 2;
});
}
}
}
return list;
好吧,也许我只是瞎了眼,但我没有找到答案:
所以,型号:
public class Dimensions
{
public int Width { get; set; }
public int Height { get; set; }
public int TypeId { get; set; }
public int PeacesForItem { get; set; }
}
我有一个过滤列表的方法:
public List<Dimensions> Consolidation(List<Dimensions> vm)
{
var list = new List<Dimensions>();
if (vm != null)
{
var typeIds = vm.Select(x => x.TypeId).ToHashSet();
foreach (var t in typeIds)
{
foreach(var item in vm)
{
if (t == item.IvericaId)
{
int x = 0;
foreach (var again in vm)
{
if (item.Duzina == again.Duzina && item.Sirina == again.Sirina && item.TypeId== again.TypeId)
{
x ++;
// vm.Remove(item); Not working, who would figure
}
}
list.Add(
new Dimensions
{
Width = item.Width,
Height = item.Height,
TypeId= item.TypeId,
PeacesForItem = x * item.PeacesForItem,
}
);
}
}
}
}
return list;
}
此方法遍历列表项并检查是否存在相同维度的元素。如果有,那就是需要的数量翻倍。
问题:此代码仍在向新列表中添加重复项,我需要将其过滤掉。
我尝试了很多方法,但我想出的每一个在设计上都有一些致命的缺陷。
复制 vm-List 并迭代复制的列表并从原始列表中删除对象
public List<Dimensions> Consolidation(List<Dimensions> vm)
{
var list = new List<Dimensions>();
if (vm != null)
{
var dublicate = new List<Dimensions>(vm);
var typeIds = vm.Select(x => x.TypeId).ToHashSet();
foreach (var t in typeIds)
{
foreach(var item in dublicate )
{
if (t == item.IvericaId)
{
int x = 0;
foreach (var again in dublicate )
{
if (item.Duzina == again.Duzina && item.Sirina == again.Sirina && item.TypeId== again.TypeId)
{
x ++;
vm.Remove(item); //Now working
}
}
list.Add(
new Dimensions
{
Width = item.Width,
Height = item.Height,
TypeId= item.TypeId,
PeacesForItem = x * item.PeacesForItem,
}
);
}
}
}
}
return list;
}
public List<Dimensions> Consolidation(List<Dimensions> vm)
{
return vm.GroupBy(d=>new {d.TypeId, d.Width, d.Height}) // if there are any duplicates, they are grouped here
.Select(g=>new Dimensions(){TypeId = g.Key.TypeId ,
Width = g.Key.Width,
Height = g.Key.Height,
PeacesForItem = g.Sum(dim=>dim.PeacesForItem)}) // number of duplicates in group calculated
.ToList();
}
你把这个问题复杂化了 :)。您不需要嵌套循环,而且您不是将值加倍,而是 平方 吗?试试这个:
var list = new List<Dimensions>();
if (vm != null)
{
foreach (var item in vm)
{
//Not sure how you identify dupes, may need to change the filter here
var duplicate = vm
.Where(v => v.PeacesForItem == item.PeacesForItem);
if (dupes.Any())
{
list.Add(new Dimension
{
Width = item.Width,
Height = item.Height,
TypeId = item.TypeId,
PeacesForItem = item.PeacesForItem * 2;
});
}
}
}
return list;