-4

I am connected to a Database using Entity Framework. Now, I want to create a List<Object> of the data received using Linq

So, I have:

var listOfSettings = new List<AppSetting>();

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

            };

How can I add that AppSetting Object to the List of Settings?

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
  • What have you tried to do so far, and what problems have you had with your current attempts to solve this problem or to find an existing solution? – Servy Jun 06 '14 at 20:48
  • No. I am trying to `listOfSettings.add(new AppSetting(){.......});` directly from the database – Luis Lavieri Jun 06 '14 at 20:48
  • 2
    If you want to add `query` to `listOfSettings`, try `listOfSettings.AddRange(query)` – crthompson Jun 06 '14 at 20:49
  • @paqogomez or, if he's just creating a new list, he could use `query.ToList()` instead. – phoog Jun 06 '14 at 20:52
  • possible duplicate of [Return list using select new in LINQ](http://stackoverflow.com/questions/6370028/return-list-using-select-new-in-linq) – Luis Lavieri Jun 06 '14 at 23:31

2 Answers2

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

                                  }).ToList();
Ryan Schlueter
  • 2,223
  • 2
  • 13
  • 19
  • Put the query between (). Something like (from appSetting...............).ToList(); –  Jun 06 '14 at 21:01
0

At the end, I had to do it like this:

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, 
                ...
            }));
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69