2014-09-29 73 views
0

如何將一個linq查詢插入到ObservableCollection數據結構中?看看我的代碼片段:Linq將選定的數據插入ObservableCollection

var server = from e in _fileElement.Elements("computerprefix") 
     where e.Attribute("name") != null 
     select new ServersLogins 
     { 
      Server = e.Attribute("name").Value, 
      Logins = from i in e.Elements("user") 
       select new ObservableCollection<Login> 
       { 
        User = i.Attribute("name"), 
        Password = i.Attribute("password") 
       } 
     }; 

的錯誤消息我有

'System.Collections.ObjectModel.ObservableCollection<CustomCopyNas.Models.Login>' does not contain a definition for 'Password' D:\c#\Samples\CustomCopyNas\CustomCopyNas\Views\LoginViewModel.cs 
'System.Collections.ObjectModel.ObservableCollection<CustomCopyNas.Models.Login>' does not contain a definition for 'User' D:\c#\Samples\CustomCopyNas\CustomCopyNas\Views\LoginViewModel.cs 

我怎麼能以正確的方式嗎?

回答

5

試試這個:

var server = from e in _fileElement.Elements("computerprefix") 
      where e.Attribute("name") != null 
      select new ServersLogins 
      { 
       Server = e.Attribute("name").Value, 
       Logins = new ObservableCollection(
           from i in e.Elements("user") 
           select new Login 
           { 
            User = i.Attribute("name"), 
            Password = i.Attribute("password") 
           }) 
      }; 
相關問題