1

我制作列表的代码是这样的:

[OperationContract]
List<it_position> GetAllPositions(){
   PositionDataClassesDataContext context = new PositionDataClassesDataContext();
   var result = from positions in context.it_positions select positions;
  return result.ToList();
}

我从一个方法中获取数据

PositionServiceReference.PositionServiceClient service = new   
     PositionServiceReference.PositionServiceClient();
service.GetAllPositionsCompleted += new   
EventHandler<PositionServiceReference.GetAllPositionsCompletedEventArgs>
          (service_GetAllPositionsCompleted);
service.GetAllPositionsAsync();

但是我怎样才能让结果成为我可以使用的数组呢?它们现在只是数据列表中的 ItemSoruces ......

void service_GetAllPositionsCompleted(object sender, 
            PositionServiceReference.GetAllPositionsCompletedEventArgs e)
{
    dgEmployee.ItemsSource = e.Result;

}
4

1 回答 1

2

如果在这一行设置断点:

dgEmployee.ItemsSource = e.Result;

并将鼠标悬停在 上e.Result,您将看到它实际上是 aIEnumrable<it_position> 如果您需要在设置之前/之后使用它,e.Result只需将其转换 - 如下所示:

void service_GetAllPositionsCompleted(object sender, 
            PositionServiceReference.GetAllPositionsCompletedEventArgs e)
{
    //var lst = e.Result as IEnumerable<it_positions>; <-- This gave error
    var lst = e.Result; // <-- This worked fine
    var ary = lst.ToArray();
    //go do fun stuff with the array
    dgEmployee.ItemsSource = e.Result;

}
于 2013-02-11T19:18:07.580 回答