Problem to use generic method inside another generic method.

  • Thread starter Thread starter Hamed_1983
  • Start date Start date
H

Hamed_1983

Guest
Hi

I have a generic method as follow :

public static T GetObjectGraph<T, TDetails, TField>(DbContext dbContext, Expression<Func<T, TDetails>> details, Expression<Func<T, TField>> field, TField value) where T : class
{
var p = field.Parameters[0];
var eq = Expression.Equal(field.Body, Expression.Constant(value));
var expr = Expression.Lambda<Func<T, bool>>(eq, p);
return dbContext
.Set<T>()
.Include(details)
.AsNoTracking()
.FirstOrDefault(expr);
}



Which using in my code as follow :

var existingPackageHeader = HelperMethods.GetObjectGraph(_dbContext, (DefaultVisitProductHeaders h) => h.DefaultVisitProductDetails, (DefaultVisitProductHeaders h) => h.DefaultVisitProductHeaderRowID, packageHeader.DefaultVisitProductHeaderRowID);

The above code works. My problem is that when i use my method in another generic method as follow :

public static void AttachDetailsListToDbContext<T, TDetails, TField>(HttpContext context, string strName, DbContext dbContext, object masterObjectGraph, Expression<Func<T, TDetails>> details, Expression<Func<T, TField>> field, TField value, InternalHelperAction action)
{
List<T> lstPackageDetails = HelperMethods.DeserializeListFromTempData<T>(context, strName);
var trackedEntries = dbContext.ChangeTracker.Entries().ToList();
foreach (var entry in trackedEntries)
entry.State = EntityState.Detached;
dbContext.Entry(existingPackageHeader).CurrentValues.SetValues(masterObjectGraph); // Compile time error!
}

I'm facing the following compile error :

The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method 'HelperMethods.GetObjectGraph(DbContext, Expression>, Expression>, TField)'

Where is the problem & how to solve it?

Thanks in advance



Database Helper v 2.0.0

Continue reading...
 
Back
Top