Get a generic ICollection property

I'm new to ASP.NET MVC 5, I need to get the Icollection property and search in another property, if the first search result is printed ok, but when I go to search in the result, it will be null.

what is the problem?

var userId = User.Identity.GetUserId();
var user = db.Users.Include( u=> u.Sucursales)
                   .Where(u => u.Id == userId)
                   .SingleOrDefault();

if( user != null )
{
    var sucursal = user.Sucursales.Include(s => s.Emisor)
                                  .Where(s => s.ID == suc)
                                  .SingleOrDefault();
    if (sucursal != null)
    {
        var tipoCfe = sucursal.Emisor.TiposCfe
                                     .Where(t => t.ID == factura)
                                     .SingleOrDefault();

      

+3


source to share


2 answers


Your order will be executed immediately after use SingleOrDefault()

, see. Fooobar.com/questions/1168040 / ... . Yours Include(s => s.Emisor)

sticks out to me though, Since Emisor was not enabled when fetching the user, you won't be able to query it since your query is no longer of type IQueryable. Your request has already been completed.

To get the required data, you will need to get the data during the first request. I would do something similar to: db.Users.Include("Sucursales.Emisor")

when you fetch the user.



More on Inclusion Method ... MSDN Inclusion Method Explained

+1


source


I changed

var user = db.Users.Include( u=> u.Sucursales)

      



for

var user = db.Users.Include("Sucursales.Emisor") 

      

0


source







All Articles