This is a migrated thread and some comments may be shown as answers.

Pass parameter to Expression<Func<TEntity, bool>> filter …

2 Answers 2723 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jesús
Top achievements
Rank 1
Jesús asked on 18 Dec 2014, 05:53 PM
I am using GenericRepository with this code ...  

public virtual IEnumerable<TEntity> Get(
    Expression<Func<TEntity, bool>> filter = null,
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    string includeProperties = "")
{
    IQueryable<TEntity> query = dbSet;
 
    if (filter != null)
    {
        query = query.Where(filter);
    }
 
    query = includeProperties.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
 
    return orderBy != null ? orderBy(query).ToList() : query.ToList();
}


And I want use this function in my Controller ...

public ActionResult Hojas_Read([DataSourceRequest] DataSourceRequest request)
{
    var usuario = _utHojas.UsuariosRepository.Get(u => u.Correo == User.Identity.Name).FirstOrDefault();
    .....
}


And in my debug window appears...

{u => (u.Correo == value(TaxiMetro.Web.Controllers.HojasController).User.Identity.Name)}

And it should be...

{u => (u.Correo == "jsanchco@gmail.com"}

 How I can fix this?

Thanks.

2 Answers, 1 is accepted

Sort by
0
Jesús
Top achievements
Rank 1
answered on 19 Dec 2014, 05:07 PM
I answer myself ...
var name = User.Identity.Name;
ParameterExpression param = Expression.Parameter(typeof (Usuario), "h");
Expression boby = Expression.Equal(Expression.PropertyOrField(param, "Correo"),
      Expression.Constant(name, typeof(string)));
Expression<Func<Usuario, bool>> filter = Expression.Lambda<Func<Usuario, bool>>(boby, param);

var usuario = _utHojas.UsuariosRepository.Get(filter).FirstOrDefault();
0
Boyan
Telerik team
answered on 22 Dec 2014, 05:01 PM
Hi Jesus,

Thank you for contacting us.

I am glad that currently things are working on your side. If you experiencing any further Telerik Data Access related issues do not hesitate to get back to us.

Regards,
Boyan
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
LINQ (LINQ specific questions)
Asked by
Jesús
Top achievements
Rank 1
Answers by
Jesús
Top achievements
Rank 1
Boyan
Telerik team
Share this question
or