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);
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; }
}