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. LINQ
  4. using Expressions [modified]

using Expressions [modified]

Scheduled Pinned Locked Moved LINQ
linqdatabasefunctionalhelpquestion
1 Posts 1 Posters 1 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.
  • E Offline
    E Offline
    enginco
    wrote on last edited by
    #1

    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 <Func<T, bool>> expr1,
                                                            <Func<T, bool>> expr2)
        {
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>
                  (Expression.Or(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.And(expr1.Body, invokedExpr), expr1.Parameters);
        }
    }
    
         public static Expression<Func<MyObj, bool>> AndKeyPredicate(MyObj o)
        {
            var predicate = PredicateBuilder.False<myobj>();
            foreach (string s in o.GetAndKeyItems()) // I need MyObj in here instead of "MyObj o" parameter
            {
                predicate = predicate.Or(p => MyText.All.Contains(s));
            }
            return predicate;
        }
    

    IQueryable<myobj> _ob = xxx.AsQueryable();

    var query = _ob.Where(AndKeyPredicate(xxx[0]));

    I must take the each MyObj item for using in AndKeyPredicate parameter. we have 2 options: 1) var query = from y in _ob where AndKeyPredicate(y) select y; it doesn't work 2) we can get instance of MyObj from public static Expression<Func<T, bool>> AndKeyPredicate() predicate know p is a MyObj: predicate = predicate.Or(p => MyText.All.Contains(s)); but I have to use MyObj before //foreach (string s in o.GetAndKeyItems()) predicate = predicate.Or(p => MyText.All.Contains(s)); HOW?????? please help me

    modified on Friday, July 18, 2008 8:14 AM

    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