3

I'm new in ASP.NET MVC 5, I need get the Icollection property and search into other property, if printed the result of the first search is fine, but when i go to search in the result is 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();
puddinman13
  • 1,408
  • 4
  • 18
  • 35
Maske
  • 824
  • 2
  • 17
  • 35
  • 1
    Could you be more specific. Which result is null? – Joonas Koski Dec 16 '14 at 20:19
  • sucursal == null, but the sucursal.ID == suc is in user.Sucursales. – Maske Dec 16 '14 at 20:20
  • Where is the value of `suc` set, and what to? – dyson Dec 16 '14 at 20:30
  • Difficult one. I see nothing wrong with the code. Are you 100% sure Sucursales contains elements and the variable 'suc' has the correct value? – Joonas Koski Dec 16 '14 at 20:30
  • Yes, 'suc' variable is fine and s.ID == suc is in user.Sucursales. Is posible that the query is executed after the if(sucursal != null)? – Maske Dec 16 '14 at 20:42
  • @Maske, the query should take place right away since you are using SingleOrDefault(), see http://stackoverflow.com/questions/18460504/single-or-default-with-condition-or-where-clause. Your Include(s => s.Emisor) sticks out to me though, since it wasn't included when fetching the user. – puddinman13 Dec 16 '14 at 21:07
  • What is `Sucursales`'s type and how is it defined in `User`? You shouldn't be able to do `user.Sucursales.Include`, because it should be `ICollection` (or similar) but not `IQueryable`. – Gert Arnold Dec 16 '14 at 21:17
  • 2
    More on include method... http://msdn.microsoft.com/en-us/library/bb738708%28v=vs.110%29.aspx You may have to do something like: db.Users.Include("Sucursales.Emisor") – puddinman13 Dec 16 '14 at 21:20

2 Answers2

1

Your query will take place right away since you are using SingleOrDefault(), see this StackOverflow question pertaining to SingleOrDefault() Your Include(s => s.Emisor) sticks out to me though. Since Emisor wasn't included when fetching the user, you will not be able to request that since your query is no longer of type IQueryable. Your query has already been executed.

In order to retrieve the data you require, you will have to obtain the data during your first query. I would do something similar to: db.Users.Include("Sucursales.Emisor") when you retrieve the user.

More on include method... MSDN Explanation of Include Method

Community
  • 1
  • 1
puddinman13
  • 1,408
  • 4
  • 18
  • 35
0

I changed

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

for

var user = db.Users.Include("Sucursales.Emisor") 
puddinman13
  • 1,408
  • 4
  • 18
  • 35
Maske
  • 824
  • 2
  • 17
  • 35