2011-04-30 72 views
0

我從WCF服務調用SQL Server 2008中的數據庫的表值函數,並將其結果存儲在LinkedList中。另一方面,當我調用這個服務並獲得這個LinkedList時,所有節點都包含相同的值。我正在使用的代碼如下:(Silverlight)返回LinkedList的WCF服務

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)); 
} 

回答

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