Get ICollection property of generic type

Maske

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get List if Properties of type ICollection from Generic Class

From Dev

Get type of property in generic method

From Dev

Access Navigation Property of type ICollection

From Dev

How to get generic property but ignore its type?

From Dev

Get properties that are of type ICollection<BaseEntity>

From Dev

generic type of class property

From Dev

Generic props in react/typescript - how to get T to be the type of a property?

From Dev

passing type of property to generic type

From Dev

Reading concrete class property within a generic method that doesn't get generic type instance

From Dev

Get generic parameters of a generic type

From Dev

Get generic parameters of a generic type

From Dev

why do we need to create new instence of Icollection type property

From Dev

why do we need to create new instence of Icollection type property

From Dev

Why generic ICollection<T> does not inherit some non-generic interface with Count property?

From Dev

Getting property type and convert generic

From Dev

Swift generic type property and method

From Dev

Generic c# property type

From Dev

JavaPoet - get generic type

From Dev

Get type of generic parameter

From Dev

Get an instance of generic type

From Dev

Get the type of generic T

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.ICollection

From Dev

Passing in a "TYPE" to a Generic_Interface type Property

From Dev

Jackson Serialization of Class with Generic Property losing type information of the generic property

From Dev

How to reference a ICollection property?

From Dev

Umbraco - Get Property by type

From Dev

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection<int> in mvc controller

From Dev

Cannot implicitly convert type 'string' to 'System.Collections.Generic.ICollection<WebApplication2.Entry>'

From Dev

Determine if anonymous type's property is a generic collection

Related Related

  1. 1

    Get List if Properties of type ICollection from Generic Class

  2. 2

    Get type of property in generic method

  3. 3

    Access Navigation Property of type ICollection

  4. 4

    How to get generic property but ignore its type?

  5. 5

    Get properties that are of type ICollection<BaseEntity>

  6. 6

    generic type of class property

  7. 7

    Generic props in react/typescript - how to get T to be the type of a property?

  8. 8

    passing type of property to generic type

  9. 9

    Reading concrete class property within a generic method that doesn't get generic type instance

  10. 10

    Get generic parameters of a generic type

  11. 11

    Get generic parameters of a generic type

  12. 12

    why do we need to create new instence of Icollection type property

  13. 13

    why do we need to create new instence of Icollection type property

  14. 14

    Why generic ICollection<T> does not inherit some non-generic interface with Count property?

  15. 15

    Getting property type and convert generic

  16. 16

    Swift generic type property and method

  17. 17

    Generic c# property type

  18. 18

    JavaPoet - get generic type

  19. 19

    Get type of generic parameter

  20. 20

    Get an instance of generic type

  21. 21

    Get the type of generic T

  22. 22

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.ICollection

  23. 23

    Passing in a "TYPE" to a Generic_Interface type Property

  24. 24

    Jackson Serialization of Class with Generic Property losing type information of the generic property

  25. 25

    How to reference a ICollection property?

  26. 26

    Umbraco - Get Property by type

  27. 27

    Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection<int> in mvc controller

  28. 28

    Cannot implicitly convert type 'string' to 'System.Collections.Generic.ICollection<WebApplication2.Entry>'

  29. 29

    Determine if anonymous type's property is a generic collection

HotTag

Archive