Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put values in a list type field

I have 2 classes and VMs. I made a method that displays typegenres, and a list of genres that are related to them, but now I need a list of TypeGenreNamesVM. I don’t understand how to pass in GenreNames a list of values Name and NameUrl. I need to make a method that displays data in a similar structure:

[
  {
   "name": "qwerty",
   "nameEng": "qwertyEng",
   "GenresNames": [
      {
       "name": "genre1",
       "nameEng": "genreEng1"
      },
      {
       "name": "genre2",
       "nameEng": "genreEng2"
      }
   ]
  }
]

Here is I have 2 classes and VMs that I using

 public class Genre
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string NameForUrl { get; set; }
        public string Description { get; set; }
        public int Fk_TypeGenreId { get; set; }
        [ForeignKey("Fk_TypeGenreId")]
        public TypeGenre TypeGenre { get; set; }
        public List<Book_Genre> Book_Genre { get; set; }
    }

    public class TypeGenre
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string NameForUrl { get; set; }
        public string Description { get; set; }
        public List<Genre> Genre { get; set; }
    }
  
    public class TypeGenreTestVM
    {
        public string Name { get; set; }
        public string NameEng { get; set; }
        public List<TypeGenreNamesVM> GenreNames { get; set; }
    }

    public class TypeGenreNamesVM
    {
        public string Name { get; set; }
        public string NameForUrl { get; set; }

    }
    public async Task<IEnumerable<TypeGenreTestVM>> GetTypesAndGenresEng()
    {
        var result = await _context.TypeGenre.Select(genres => new TypeGenreTestVM()
        {
            Name = genres.Name,
            NameEng = genres.NameForUrl,
            GenreNames = 

        }).ToListAsync();

        return result;
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Almost the same as the first Select

public async Task<IEnumerable<TypeGenreTestVM>> GetTypesAndGenresEng()
{
    var result = await _context.TypeGenre.Select(genres => new TypeGenreTestVM()
    {
        Name = genres.Name,
        NameEng = genres.NameForUrl,
        GenreNames = genres.Genre.Select(g => new TypeGenreNamesVM
        {
            Name = g.Name,
            NameEng = g.NameForUrl 
        })
        .ToList()
    }).ToListAsync();

    return result;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading