Search from a property in a list of a certain class

Asked

Viewed 26 times

-1

I am trying to filter the results from a property of a class, within a list. Today I have the following structure :

public class Jogo
    {
        public int Id { get; set; }
        public string Titulo { get; set; }
        public List<Jogador> Jogadores  { get; set; }
    }

My player class has the following properties:

        public int id { get; set; }
        public string Nome { get; set; }
        public int NumeroCamisa { get; set; }

Initially, my list contained only one string. With this, I can use Contains.

return await _context.jogos.AsQueryable()
                    .Where(x => x.Titulo.Contains("nome do jogo") 
                        && x.informacoes.Contains("numero da camisa) 
                        || x.informacoes.Contains("nome do jogador")
                    .ToListAsync();

It turns out after I typed the list for the player class I came across the following mistake:

Argument 1: cannot convert from 'string' to 'Model.Jogador'.

I intend to search for the game title, player name and shirt number.

I looked for other topics and I’m facing a lot of difficulty in doing so in a way that I can search for a game by the title or name or number of the shirt.

I tried using Linq with Where, Contains, Find.

  • 1

    Buenas amigo, I believe you should adjust the query to search inside the Player object, more or less asism: Return await _context.jogos.Asqueryable() . Where(x => x.Titulo.Contains("game name") && x.Jogadores.Firstordefault(j => j.nome.Contains("player name" || j.Numerocamisa == 12) != null). Tolistasync();

1 answer

0

Good morning!

I did it this way, I used the Any to access the list, and the Contains to search within it :

return await _context.jogos.AsQueryable()
                .Where(x => x.Titulo.Contains("nome do jogo") 
                    || x. Jogadores.Any(x => x.Nome.Contains("nome jogador") 
                .ToListAsync();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.