IQueryable select data from multiple tables

2

I have a queryable linq to entities ) in the EntityFramework and I am 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 that I will use, they are: the properties m.PK_GRUPO and m.DS_GRUPO are OK, both are of the string type since m.SUBGRUPO is an ICollection of the SubGroup class and I also need to get m.SUBGRUPO.DS_SUBGRUPO that I am not getting.

Does anyone know how I can get this other property?

    
asked by anonymous 15.08.2014 / 21:49

1 answer

1

I do not quite understand this part here:

var model = _service.List()...

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

The command in theory is right. Some increments are missing. I will take some liberties to provide an answer that is more aligned with the standard mode of use of the Entity Framework:

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();
    
15.08.2014 / 22:22