The type must be a reference type to be used as a parameter

Ivan-Mark Debono

I get this compile error

The type 'TEntiy' must be a reference type in order to use it a parameter 'TEntity' in the generic type or method 'DbSet'.

When used in this method:

private IQueryable<TEntity> Search<Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] include)
{
    var dbSet = dataContext.Set<TEntity>();

    var set = include.Aggregate<Expression<Func<TEntity, object>>, IQueryable<TEntity>>
                (dbSet, (current, expression) => current.Include(expression));

    return set.Where(predicate);
}

Is there a way to fix this problem?

Peter van der Heijden

The DbSet<TEntity> type requires its type argument to be a reference type. You can ensure this is the case by adding a generic type constraint:

private IQueryable<TEntity> Search<TEntity>(...) where TEntity : class
{
    ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

An optional parameter must be a reference type, a nullable type, or be declared as an optional

From Dev

Why do the coherence rules raise the error "the type parameter must be used as the type parameter for some local type"?

From Dev

error: type parameter `D` must be used as the type parameter for some local type

From Dev

When listIterator is used as a reference type, the object assigned to it must implement it?

From Dev

The type 'short' must be a reference type in order to use it as parameter 'TTargetEntity' in the generic type

From Dev

The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to ''

From Dev

The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference

From Dev

What does "The type T must be a reference type in order to use it as parameter" mean?

From Dev

Asp.Net MVC 5 - The type 'bool' must be a reference type in order to use it as parameter

From Dev

Generic method - Type cannot be used as Type Parameter

From Dev

ref keyword with reference type parameter

From Dev

How to reference a subclass as a type parameter?

From Dev

Passing a reference type as a parameter by ref

From Dev

Type of argument to keys on reference must be unblessed

From Dev

Arrays used as indices must be of integer (or boolean) type

From Dev

Why isn't it a reference type when std::initializer_list being used as an assignment parameter?

From Dev

Why isn't it a reference type when std::initializer_list being used as an assignment parameter?

From Dev

The type 'T' cannot be used as type parameter 'T' in the generic type or method

From Dev

Type cannot be used as type parameter 'T' in the generic type or method - Why?

From Dev

Java: type parameter must implement interface

From Dev

Template non-type arguments for reference type and odr-used

From Dev

Non-type reference parameter/argument

From Dev

Use of type parameter in Java method reference

From Dev

Pass a type parameter to be used as argument LabelledGeneric

From Dev

Confused with usage of type parameter used in scala

From Dev

Is a String generic parameter treated as a value-type or reference-type?

From Dev

C++: template function with explicitly specified reference type as type parameter

From Dev

Repository cannot be used as a type parameter in the generic type of method

From Dev

Why a value of type null cannot be used as a default parameter with type double?

Related Related

  1. 1

    An optional parameter must be a reference type, a nullable type, or be declared as an optional

  2. 2

    Why do the coherence rules raise the error "the type parameter must be used as the type parameter for some local type"?

  3. 3

    error: type parameter `D` must be used as the type parameter for some local type

  4. 4

    When listIterator is used as a reference type, the object assigned to it must implement it?

  5. 5

    The type 'short' must be a reference type in order to use it as parameter 'TTargetEntity' in the generic type

  6. 6

    The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to ''

  7. 7

    The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference

  8. 8

    What does "The type T must be a reference type in order to use it as parameter" mean?

  9. 9

    Asp.Net MVC 5 - The type 'bool' must be a reference type in order to use it as parameter

  10. 10

    Generic method - Type cannot be used as Type Parameter

  11. 11

    ref keyword with reference type parameter

  12. 12

    How to reference a subclass as a type parameter?

  13. 13

    Passing a reference type as a parameter by ref

  14. 14

    Type of argument to keys on reference must be unblessed

  15. 15

    Arrays used as indices must be of integer (or boolean) type

  16. 16

    Why isn't it a reference type when std::initializer_list being used as an assignment parameter?

  17. 17

    Why isn't it a reference type when std::initializer_list being used as an assignment parameter?

  18. 18

    The type 'T' cannot be used as type parameter 'T' in the generic type or method

  19. 19

    Type cannot be used as type parameter 'T' in the generic type or method - Why?

  20. 20

    Java: type parameter must implement interface

  21. 21

    Template non-type arguments for reference type and odr-used

  22. 22

    Non-type reference parameter/argument

  23. 23

    Use of type parameter in Java method reference

  24. 24

    Pass a type parameter to be used as argument LabelledGeneric

  25. 25

    Confused with usage of type parameter used in scala

  26. 26

    Is a String generic parameter treated as a value-type or reference-type?

  27. 27

    C++: template function with explicitly specified reference type as type parameter

  28. 28

    Repository cannot be used as a type parameter in the generic type of method

  29. 29

    Why a value of type null cannot be used as a default parameter with type double?

HotTag

Archive