2014-06-06 66 views
-4

我使用Entity Framework連接到數據庫。現在,我想創建的數據的List<Object>使用Linq使用Linq從數據庫填充對象

收到所以,我有:

var listOfSettings = new List<AppSetting>(); 

var query = from appSetting in AppSettings 
      where appSetting.AppConfigID == appConfigId 
      select new AppSetting() 
      { 
       AppSettingID = Int16.Parse(appSetting.AppSettingID), 
       ... 

      }; 

我如何可以添加AppSetting Object設置的List

+0

你嘗試過什麼到目前爲止做了,有什麼問題,你有沒有跟你目前試圖解決這個問題,或者找到現有的解決方案? – Servy

+0

不,我想'listOfSettings.add(new AppSetting(){.......});'直接從數據庫 –

+2

如果你想向'listOfSettings'添加'query',請嘗試'listOfSettings。 AddRange(查詢)' – paqogomez

回答

2
List<AppSetting> listOfSettings = (from appSetting in AppSettings 
            where appSetting.AppConfigID == appConfigId 
            select new AppSetting() 
            { 
             AppSettingID = Int16.Parse(appSetting.Name), 
             ... 

            }).ToList(); 
+0

沒有工作。無法解析符號'ToList()' –

+0

將查詢放在()之間。類似於(來自appSetting ...............)。ToList(); – Areks

+0

明白了。謝謝。 –

0

最後,我不得不這樣做是這樣的:

var query = (AppSettings.Select(appSetting => new 
      { 
       AppSettingID = appSetting.AppSettingID, 
       ... 
      }).Where(appSetting => appSetting.AppConfigID == appConfigId)).ToList(); 

      listOfAppSettings.AddRange(query.Select(setting => new AppSetting() 
      { 
       AppSettingID = setting.AppSettingID, 
       ... 
      }));