Her*_*Giz 1 c# asp.net-mvc logic
好吧,也许我只是瞎了,但回答是在逃避我:
所以,型号:
public class Dimensions
{
public int Width { get; set; }
public int Height { get; set; }
public int TypeId { get; set; }
public int PeacesForItem { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个过滤List的方法:
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;
}
Run Code Online (Sandbox Code Playgroud)
此方法通过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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |