gpt4 book ai didi

c# - Lambda 表达式和 InvokeOperation

转载 作者:太空宇宙 更新时间:2023-11-03 19:24:14 24 4
gpt4 key购买 nike

我有这段代码,但不知道如何通过 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
{
}
}

最佳答案

你的问题有点太宽泛,所以我在这里猜测,但 lambda 表达式的唯一地方是 Hook 到事件中。因此,您可以这样做,而不是使用单独的 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
{
}
}
}

关于c# - Lambda 表达式和 InvokeOperation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9978564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com