如何包含列表中的对象,另一个对象中的对象?

时间:2019-03-26 09:18:18

标签: c# linq asp.net-core lambda

我需要检查(对于过滤器)对象内部的名称是否为输入的名称。

我有一个名为AppUser的模型,它是IdentityUser。 AppUser包含一个名为UsersData的对象。在UsersData内部,有一个Languages对象列表。在“语言”对象内部,有一个名为LanguageName的对象。在LanguageName内部,是我需要比较的名称。

我需要直接从AppUser访问它。我尝试包含数据,但可以包含的语言列表最多,但不能包含列表中的字段。

我尝试过使用thenInclude,但是由于它是一个列表,因此我不能使用theninclude。

AppUser模型:

    public class AppUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }
        public UsersData UsersData { get; set; }

    }

UsersData模型:

public class UsersData
    {
        public Guid Id { get; set; }
        public Gender Gender { get; set; }
        public City City { get; set; }
        public Company Company { get; set; }
        public Line Line { get; set; }
        public Area Area { get; set; }
        public List<AcademicRecord> AcademicRecords { get; set; }
        public List<MasterPostgraduate> MasterPostgraduates { get; set; }
        public List<Language> Languages { get; set; }
        public List<Technology> Technologies { get; set; }
        public List<Project> Projects { get; set; }
        public List<ProfessionalRecord> ProfessionalRecords { get; set; }
    }

语言模型:

    public class Language
    {
        public Guid Id { get; set; }
        public LanguageName LanguageName { get; set; }
        public CertificationFile CertificationFile { get; set; }
        public int WritingLevel { get; set; }
        public int SpeakingLevel { get; set; }
        public int CertificationLevel { get; set; }
        public string CertificationName { get; set; }
        public DateTime CertificationDate { get; set; }
        public UsersData UserInfo { get; set; }
    }

LanguageName型号:

    public class LanguageName
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }

我尝试包含数据的方式:

            List<AppUser> users = await _userManager.Users
                .Include(x => x.UsersData.Technologies)
                .Include(y => y.UsersData.AcademicRecords)
                .Include(z => z.UsersData.Languages)
                .ToListAsync();

目前,technologyName为空。

if(technoName != "")
{
    users = users.Where(x => x.UsersData.Technologies.Any(item => item.TechName.Name == technoName)).ToList();
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

尝试以下操作:

    class Program
    {

        static void Main(string[] args)
        {
            string technoName = "abc";
            UserManager _userManager = new UserManager();

            AppUser appUser = new AppUser() { Users = _userManager.Users.ToList() };

            var users = appUser.Users.SelectMany(x => x.Languages.Where(y => y.LanguageName.Name == technoName)).ToList();

        }

    }
    public class AppUser
    {
        public List<UsersData> Users { get; set; }
    }
    public class UserManager
    {
        public List<UsersData> Users { get; set; }
    }
    public class UsersData
    {
        public Guid Id { get; set; }
        //public Gender Gender { get; set; }
        //public City City { get; set; }
        //public Company Company { get; set; }
        //public Line Line { get; set; }
        //public Area Area { get; set; }
        //public List<AcademicRecord> AcademicRecords { get; set; }
        //public List<MasterPostgraduate> MasterPostgraduates { get; set; }
        public List<Language> Languages { get; set; }
        //public List<Technology> Technologies { get; set; }
        //public List<Project> Projects { get; set; }
        //public List<ProfessionalRecord> ProfessionalRecords { get; set; }
    }
    public class Language
    {
        public Guid Id { get; set; }
        public LanguageName LanguageName { get; set; }
        //public CertificationFile CertificationFile { get; set; }
        public int WritingLevel { get; set; }
        public int SpeakingLevel { get; set; }
        public int CertificationLevel { get; set; }
        public string CertificationName { get; set; }
        public DateTime CertificationDate { get; set; }
        public UsersData UserInfo { get; set; }
    }
    public class LanguageName
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }

答案 1 :(得分:0)

即使是列表,我也可以使用thenInclude,但我可能第一次拼写错误。