(Silverlight)WCF服务返回LinkedList

时间:2011-04-30 11:42:35

标签: wcf silverlight-4.0

我从WCF服务调用SQL Server 2008中的数据库的表值函数,并将其结果存储在LinkedList中。另一方面,当我调用此服务并获取此LinkedList时,所有节点都包含相同的值。我使用的代码如下:

public class user
{
    public string name;
    public string email;
}

public class ChatService : IChatService
{

 public LinkedList<user> getOnlineList(string email)
    {
        LinkedList<user> online = new LinkedList<user>();
        JabsBaseDataContext db = new JabsBaseDataContext();
        IQueryable<onlineListResult> justOnline = db.onlineList(email);
        foreach (onlineListResult r in justOnline)
        {
            user os = new user()
            {
                name = r.fname + " " + r.lname,
                email = r.email
            };
            online.AddLast(os);
        }
        return online;
    }
}

chatService.getOnlineListCompleted += onlineRetrieved;
chatService.getOnlineListAsync(email);
void onlineRetrieved(object sender, getOnlineListCompletedEventArgs e)
{
   foreach(user us in e.Result)
   this.dispatcher.BeginInvoke(() => MessageBox.show(us.name+"\n"+us.email));
}

1 个答案:

答案 0 :(得分:0)

在onlineRetrieved中,更改foreach循环中的代码以使用本地var来保存用户并使用它。即改变

foreach(user us in e.Result)
   this.dispatcher.BeginInvoke(() => MessageBox.show(us.name+"\n"+us.email));

foreach(user us in e.Result)
{
 user currentUser = us;
   this.dispatcher.BeginInvoke(() => MessageBox.show(currentUser.name+"\n"+currentUser.email));
}

请参阅此原因 - Problem with anonymouse delegate within foreach