PredicateBuilder Usage
-
I've never used PredicateBuilder before. If anyone has used it, I could use some help
List<PurchasingEntity> results = null;
IQueryable<PurchasingEntity> query = (from c in db.Companies
join p in db.Projects on c.Id equals p.CompanyId
join j in db.Jobs on p.Id equals j.ProjectId
select new PurchasingEntity
{
CompanyId = c.Id,
ProjectId = p.Id,
JobId = j.Id,
Phase = j.Phase,
Quantity = j.Quantity.HasValue ? j.Quantity.Value : 0,
StartDate = j.StartDate.HasValue ? j.StartDate : null
});var predicate = PredicateBuilder.True<Purchasing>();
if (companyId > 0)
{
predicate = predicate.And(i => i.CompanyId == companyId);
}if (projectId > 0)
{
predicate = predicate.And(i => i.ProjectId == projectId);
}if (startDate != null)
{
predicate = predicate.And(i => i.StartDate >= startDate);
}if (endDate != null)
{
predicate = predicate.And(i => i.StartDate <= endDate);
}results = query.Where(predicate); // <===== ERROR HERE
return results.ToList();The compilation error is
cannot convert from 'System.Linq.Expressions.Expression<System.Func<Jayhawk.DAL.DataContext.Purchasing, bool>>' to 'System.Linq.Expressions.Expression<System.Func<Jayhawk.Entities.PurchasingEntity, int, bool>>'
Not quitre sure what's wrong here? Can anyone help?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I've never used PredicateBuilder before. If anyone has used it, I could use some help
List<PurchasingEntity> results = null;
IQueryable<PurchasingEntity> query = (from c in db.Companies
join p in db.Projects on c.Id equals p.CompanyId
join j in db.Jobs on p.Id equals j.ProjectId
select new PurchasingEntity
{
CompanyId = c.Id,
ProjectId = p.Id,
JobId = j.Id,
Phase = j.Phase,
Quantity = j.Quantity.HasValue ? j.Quantity.Value : 0,
StartDate = j.StartDate.HasValue ? j.StartDate : null
});var predicate = PredicateBuilder.True<Purchasing>();
if (companyId > 0)
{
predicate = predicate.And(i => i.CompanyId == companyId);
}if (projectId > 0)
{
predicate = predicate.And(i => i.ProjectId == projectId);
}if (startDate != null)
{
predicate = predicate.And(i => i.StartDate >= startDate);
}if (endDate != null)
{
predicate = predicate.And(i => i.StartDate <= endDate);
}results = query.Where(predicate); // <===== ERROR HERE
return results.ToList();The compilation error is
cannot convert from 'System.Linq.Expressions.Expression<System.Func<Jayhawk.DAL.DataContext.Purchasing, bool>>' to 'System.Linq.Expressions.Expression<System.Func<Jayhawk.Entities.PurchasingEntity, int, bool>>'
Not quitre sure what's wrong here? Can anyone help?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
List<PurchasingEntity> results = null;
IQueryable<PurchasingEntity> query =
var predicate = PredicateBuilder.True<Purchasing>();
results = query.Where(predicate); // <===== ERROR HERE
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
I've never used PredicateBuilder before. If anyone has used it, I could use some help
List<PurchasingEntity> results = null;
IQueryable<PurchasingEntity> query = (from c in db.Companies
join p in db.Projects on c.Id equals p.CompanyId
join j in db.Jobs on p.Id equals j.ProjectId
select new PurchasingEntity
{
CompanyId = c.Id,
ProjectId = p.Id,
JobId = j.Id,
Phase = j.Phase,
Quantity = j.Quantity.HasValue ? j.Quantity.Value : 0,
StartDate = j.StartDate.HasValue ? j.StartDate : null
});var predicate = PredicateBuilder.True<Purchasing>();
if (companyId > 0)
{
predicate = predicate.And(i => i.CompanyId == companyId);
}if (projectId > 0)
{
predicate = predicate.And(i => i.ProjectId == projectId);
}if (startDate != null)
{
predicate = predicate.And(i => i.StartDate >= startDate);
}if (endDate != null)
{
predicate = predicate.And(i => i.StartDate <= endDate);
}results = query.Where(predicate); // <===== ERROR HERE
return results.ToList();The compilation error is
cannot convert from 'System.Linq.Expressions.Expression<System.Func<Jayhawk.DAL.DataContext.Purchasing, bool>>' to 'System.Linq.Expressions.Expression<System.Func<Jayhawk.Entities.PurchasingEntity, int, bool>>'
Not quitre sure what's wrong here? Can anyone help?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Bugger that looked really useful but it is in c#7, VS 2017. We are stuck with 2015.
Never underestimate the power of human stupidity RAH
-
I've never used PredicateBuilder before. If anyone has used it, I could use some help
List<PurchasingEntity> results = null;
IQueryable<PurchasingEntity> query = (from c in db.Companies
join p in db.Projects on c.Id equals p.CompanyId
join j in db.Jobs on p.Id equals j.ProjectId
select new PurchasingEntity
{
CompanyId = c.Id,
ProjectId = p.Id,
JobId = j.Id,
Phase = j.Phase,
Quantity = j.Quantity.HasValue ? j.Quantity.Value : 0,
StartDate = j.StartDate.HasValue ? j.StartDate : null
});var predicate = PredicateBuilder.True<Purchasing>();
if (companyId > 0)
{
predicate = predicate.And(i => i.CompanyId == companyId);
}if (projectId > 0)
{
predicate = predicate.And(i => i.ProjectId == projectId);
}if (startDate != null)
{
predicate = predicate.And(i => i.StartDate >= startDate);
}if (endDate != null)
{
predicate = predicate.And(i => i.StartDate <= endDate);
}results = query.Where(predicate); // <===== ERROR HERE
return results.ToList();The compilation error is
cannot convert from 'System.Linq.Expressions.Expression<System.Func<Jayhawk.DAL.DataContext.Purchasing, bool>>' to 'System.Linq.Expressions.Expression<System.Func<Jayhawk.Entities.PurchasingEntity, int, bool>>'
Not quitre sure what's wrong here? Can anyone help?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
It will cause an error because you are assigning IQueryable to List. You can try this:
results = query.Where(predicate).ToList(); // <===== TO LIST HERE
return resultsI didn't used PredicateBuilder but i used System.Linq.Dynamic to create dynamic filtering or order by.
[Signature space for sale]
-
Bugger that looked really useful but it is in c#7, VS 2017. We are stuck with 2015.
Never underestimate the power of human stupidity RAH
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>> (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters); } public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>> (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters); }
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
List<PurchasingEntity> results = null;
IQueryable<PurchasingEntity> query =
var predicate = PredicateBuilder.True<Purchasing>();
results = query.Where(predicate); // <===== ERROR HERE
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
I must have stared at that for an hour. Just didn't see it
var predicate = PredicateBuilder.True<PurchasingEntity>();
.
.
.
results = query.Where(predicate).ToList();This works now Thanks!
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.