Iqueryable select data from multiple tables

Asked

Viewed 526 times

2

I have a queryable (Linq to entities) in the Entityframework and I’m not able to pull data from two or more tables.

For example:

var model = _service.List().Where(m => m.DS_GRUPO.Contains(searchString)).Select(m => new { m.PK_GRUPO, m.DS_GRUPO, m.SUBGRUPO.DS_SUBGRUPO }).ToList();

Here I do a search and from the search result I select only the data I will use, they are: the properties m.PK_GRUPO and m.DS_GRUPO are OK, both are string type already a m SUBGROUP is a Icollection of the Subgroup class and need to take also m.SUBGRUPO.DS_SUBGRUPO that I’m not getting.

Does anyone know how I can get this other property?

1 answer

1

I don’t quite understand this part here:

var model = _service.List()...

But I suppose it should be a context call, since you put that code works in Entity Framework.

The command in theory is correct. Some increments are missing. I will take a few liberties to provide an answer that is more aligned with the Entity Framework’s default mode of use:

var model = context.Grupos
                .Include(m => m.Subgrupo)
                .Where(m => m.DS_GRUPO.Contains(searchString))
                .Select(m => new { m.PK_GRUPO, m.DS_GRUPO, m.SUBGRUPO.DS_SUBGRUPO })
                .ToList();
  • The Include method does not appear! The people here in the service use a service that instantiates a repository. So _service.List().

  • Got it. Some method returns a DbSet? You can ask your question about the repository implementation?

  • I figured out the mistake, I made a mix-up in setting the parameters. In case I need to list the Group and not in the Subgroup, because the Group should contain a list of sub groups. Thanks anyway! How should I proceed to finalize this question? I request the exclusion or put the answer?

  • @Trxplz0 Post your reply and mark it as accepted.

Browser other questions tagged

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