0

I am a beginner learning linq. How to query a list object using linq

var dbUserSettings = new List<UserSetting> {  
    new UserSetting { 
        UserId = "abcxyz@123", 
        Applications = new List<Application> { 
            new Application { 
                ApplicationID = "application123", 
                ApplicationName = "ProtocolArchiving", 
                Settings = new List<Setting> { 
                    new Setting { 
                        SettingId = "setting123", 
                        SettingKey = "RefreshInterval", 
                        SettingValue = "20", 
                        IsActive = "true", 
                        UpdatedOn = "2017-06-22", 
                        SettingLabel = "PageRefresh" } } } } },

    new UserSetting { 
        UserId = "abcxyz@345", 
        Applications = new List<Application> { 
            new Application { 
                ApplicationID = "application345", 
                ApplicationName = "ProtocolArchiving", 
                Settings = new List<Setting> { 
                    new Setting { 
                    SettingId = "setting456", 
                        SettingKey = "UploadSetting", 
                        SettingValue = "20", 
                        IsActive = "true", 
                        UpdatedOn = "2017-06-22", 
                        SettingLabel = "Upload" } } } } },
    new UserSetting { 
        UserId = "abcxyz@567", 
        Applications = new List<Application> { 
            new Application { 
                ApplicationID = "application678", 
                ApplicationName = "ProtocolArchiving", 
                Settings = new List<Setting> { 
                    new Setting { 
                        SettingId = "setting789", 
                        SettingKey = "DownloadSetting", 
                        SettingValue = "20", 
                        IsActive = "true", 
                        UpdatedOn = "2017-06-22", 
                        SettingLabel = "Download" } } } } }
     };

 var response = dbUserSettings.Where(e => e.UserId == userID)
                   .Select(dbsetting => new UserSettingViewModel
                   {
                       SettingKey = dbsetting.Applications.Single<Setting>(s=> s == )

                   })
                   .ToArray();

I am querying for settingkey which matches with my userID.

Edit:

Missed couple of things to mention. I have tried few things here

SettingKey = dbsetting.Applications.FirstOrDefault().Select(sk => sk.Settings.FirstOrDefault()?.SettingKey);

The error is as belowenter image description here

My application class looks like this.

public class Application
{
    public string ApplicationID { get; set; }
    public string ApplicationName { get; set; }
    public List<Setting> Settings { get; set; }
}
Fabiano
  • 5,124
  • 6
  • 42
  • 69
wandermonk
  • 6,856
  • 6
  • 43
  • 93

1 Answers1

1

As you´re just interested in the one and only Application for every UserSetting I suppose you need this:

var response = dbUserSettings.Where(e => e.UserId == userID)
    .Select(dbsetting => new UserSettingViewModel
    {
         SettingKey = dbsetting.Applications.FirstOrDefault()?.Settings.FirstOrDefault()?.SettingKey
    };

This will just return the very first (and probably only) Setting within the very first (and propbably also only) Application within every UserSetting. If any of the lists in between in empty (either Applications or Settings) the SettingKey will be null.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111