使用Linq从数据库填充对象

时间:2014-06-06 20:46:23

标签: c# linq

我使用Entity Framework连接到数据库。现在,我想使用List<Object>

创建一个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

2 个答案:

答案 0 :(得分:2)

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

                                  }).ToList();

答案 1 :(得分: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, 
                ...
            }));