我有这段代码,无法弄清楚如何通过Lambda表达式简化它?
public void LoadEntities(QueryBuilder<Device> query, Action<ServiceLoadResult<Device>> callback, object state)
{
InvokeOperation<List<DivisionHierarchy>> obj = this.Context.GetAllDivisions();
obj.Completed += new EventHandler(obj_Completed);
}
void obj_Completed(object sender, EventArgs e)
{
try
{
if (sender is InvokeOperation<List<DivisionHierarchy>>)
{
ObservableCollection<DivisionHierarchy> divisions =
new ObservableCollection<DivisionHierarchy>((sender as InvokeOperation<List<DivisionHierarchy>>).Value);
}
}
catch
{
}
}
答案 0 :(得分:6)
你的问题有点过于宽泛,所以我在这里猜测,但这里lambda表达式的唯一地方是挂钩事件。因此,您可以执行以下操作,而不是使用单独的obj_Completed方法:
public void LoadEntities(QueryBuilder<Device> query, Action<ServiceLoadResult<Device>> callback, object state)
{
InvokeOperation<List<DivisionHierarchy>> obj = this.Context.GetAllDivisions();
obj.Completed += (sender, e) =>
{
try
{
if (sender is InvokeOperation<List<DivisionHierarchy>>)
{
ObservableCollection<DivisionHierarchy> divisions =
new ObservableCollection<DivisionHierarchy>((sender as InvokeOperation<List<DivisionHierarchy>>).Value);
}
}
catch
{
}
}
}
答案 1 :(得分:0)
所以最终解决方案就在这里
public void LoadEntities(QueryBuilder<Device> query, Action<ServiceLoadResult<Device>> callback, object state)
{
((InvokeOperation<List<DivisionHierarchy>>)this.Context.GetAllDivisions()).Completed += (sender, e) =>
{
try
{
if (sender is InvokeOperation<List<DivisionHierarchy>>)
{
ObservableCollection<DivisionHierarchy> divisions =
new ObservableCollection<DivisionHierarchy>((sender as InvokeOperation<List<DivisionHierarchy>>).Value);
}
}
catch
{
}
};
}