Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. PredicateBuilder Usage

PredicateBuilder Usage

Scheduled Pinned Locked Moved C#
helpdatabaselinqcsharpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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.

    L M K 3 Replies Last reply
    0
    • K Kevin Marois

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • K Kevin Marois

        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.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        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

        K 1 Reply Last reply
        0
        • K Kevin Marois

          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.

          K Offline
          K Offline
          Keviniano Gayo
          wrote on last edited by
          #4

          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 results

          I didn't used PredicateBuilder but i used System.Linq.Dynamic to create dynamic filtering or order by.

          [Signature space for sale]

          1 Reply Last reply
          0
          • M Mycroft Holmes

            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

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • L Lost User

              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

              K Offline
              K Offline
              Kevin Marois
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups