0
我是初学linq的学习者。如何使用linq查询列表对象使用linq查询嵌套列表
var dbUserSettings = new List<UserSetting> {
new UserSetting {
UserId = "[email protected]",
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 = "[email protected]",
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 = "[email protected]",
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();
我正在查询与我的userID匹配的settingkey。
编辑:
错过了一些事情提及。我在这里
SettingKey = dbsetting.Applications.FirstOrDefault().Select(sk => sk.Settings.FirstOrDefault()?.SettingKey);
试过几件事情我的应用程序类看起来是这样的。
public class Application
{
public string ApplicationID { get; set; }
public string ApplicationName { get; set; }
public List<Setting> Settings { get; set; }
}
'我正在查询与我的userID匹配的settingkey。'对你有好处,那究竟是什么问题? – EpicKip
你的问题是什么?任何意外的结果?错误?请更具体地说明你想要做什么。 – HimBromBeere
SettingKey = dbsetting.Applications.FirstOrDefault()?. Select(sk => sk.Settings.FirstOrDefault()?. SettingKey) –