如何重构我的下面的C#代码以使我的流程获得更好的性能?
var list= new List<TEST>();
foreach(var id in ids)
{
var list1= helper.method(id);
foreach(var path in list)
{
var match = list1.Any(s => s.ChildId == path.ChildId);
if (!match)
{
list1.Add(path);
}
}
}
答案 0 :(得分:1)
因此,您希望根据getElement = (id) ->
document.getElementById(id)
titleInput = getElement 'id_title'
subscriptionInput = getElement 'id_subscription'
submitButton = getElement 'submit'
canvas = new fabric.Canvas 'canvas', {backgroundColor: 'rgb(255,255,255)'}
saveImage = (e) ->
subscription = subscriptionInput.value
title = titleInput.value
image = canvas.toDataURL {format: 'jpeg',quality: 0.8}
data = {title: title, subscription: subscription, image: image}
$.ajax {type: 'POST', url: '', data: data}
submitButton.addEventListener 'click', saveImage, false
?
您可以使用自定义ChildId
,IEqualityComparer<ClassName>
和Distinct
。
例如(假设SelectMany
是你的班级):
Demo
现在,您可以在许多LINQ扩展方法中使用此比较器,例如public class Demo
{
public int ID { get; set; }
}
public class DemoComparer : IEqualityComparer<Demo>
{
public bool Equals(Demo x, Demo y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return x.ID == y.ID;
}
public int GetHashCode(Demo obj)
{
return obj.ID;
}
}
或GroupBy
:
Distinct
答案 1 :(得分:1)
怎么样:
Dictionary<string, TEST> t = helper.GETID(id)
.ToDictionary(path => path.ChildId, path => path);
如果你真的需要List<TEST>
,你可以这样做:
List<TEST> list = t.Select(kv => kv.Value).ToList();
如果你有太多元素,你可以依赖并行处理:
Dictionary<string, TEST> dic= helper.GETID(id)
.AsParallel()
.ToDictionary(path => path.ChildId, path => path);