首页 > 解决方案 > 优化 linq 查询的更好方法

问题描述

我在 C# 应用程序中编写了一个 linq 查询来获取 auth 用户所属的应用程序的名称

var authApplicationRepository = AuthApplicationRepository.GetAllAsList();
var authUserRepository = AuthUserRepository.GetAllAsList();

你认为有没有更好更短的方法来结合这两条线

//Get the name of the application the auth user belongs to
var authUserApplicationId = authUserRepository
                            .Where(x => x.Id == userChangeRequest.AuthUserId)
                            .Select(x => x.ApplicationId)
                            .FirstOrDefault();

var authUserApplicationName = authApplicationRepository
                              .Where(x => x.Id == authUserApplicationId)
                              .Select(x => x.Description)
                              .FirstOrDefault();

标签: c#linq

解决方案


推荐阅读