daily pastebin goal
81%
SHARE
TWEET

Untitled

a guest Nov 20th, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public Expression<Func<EmailAflAwmMessageDM, bool>> SearchXpr(string param, string q)
  2. {
  3.     if (param == "to")
  4.         return e => e.to_msg.Contains(q);
  5.     else if (param == "from")
  6.         return e => e.from_msg.Contains(q);
  7.     else if (param == "cc")
  8.         return e => e.cc_msg.Contains(q);
  9.     else if (param == "bcc")
  10.         return e => e.bcc_msg.Contains(q);
  11.     else if (param == "subject")
  12.         return e => e.subject.Contains(q);
  13.     else
  14.         return e => e.body_text.Contains(q);
  15. }
  16.    
  17. public Expression<Func<EmailAflAwmMessageDM, bool>> FiltertXpr(string filter, string value)
  18.    {
  19.     if (filter == "attachments")
  20.     return e => e.attachments == value;
  21.     else if (filter == "flagged")
  22.     return e => e.flagged == value;
  23.     else
  24.     return e => e.seen == value;
  25.     }
  26.    
  27. private IQueryable SearchFilter(string param,string q,string filter,
  28.        string value,string sort,string dir)
  29.    {
  30.       var searchXpr = SearchXpr(param, q);
  31.       var filterXpr = FiltertXpr(filter, value);
  32.       var emailmessage =
  33.       db.EmailAflAwmMessage.
  34.       Where(filterXpr).Where(searchXpr)
  35.       .OrderByDescending(a => a.msg_date).Select(a =>
  36.       new
  37.       {
  38.        a.subject,
  39.        a.msg_date,
  40.       });
  41.  
  42.        return emailmessage;
  43.       }
  44.    
  45. public static class ExpressionBuilder
  46.     {
  47.         private static readonly MethodInfo ToStringMethod = typeof(object).GetMethod("ToString");
  48.         private static readonly MethodInfo StringContainsMethod = typeof(string).GetMethod("Contains");
  49.  
  50.         public static Func<T, object> Selector<T>(string prop)
  51.         {
  52.             var type = typeof(T);
  53.             var param = Expression.Parameter(type);
  54.             return Expression.Lambda<Func<T, object>>(Expression.Property(param, type.GetProperty(prop)), param).Compile();
  55.         }
  56.  
  57.         public static Expression<Func<T, bool>> BuildFilterPredicate<T>(string q)
  58.         {
  59.             var query = Expression.Constant(q);
  60.             var type = typeof(T);
  61.             var lbdSelector = Expression.Parameter(type);
  62.             var predicates = type.GetProperties().SelectMany(p => PredicateContainsBuilder(lbdSelector, p, query)).ToList();
  63.             Expression body = predicates[0];
  64.             body = predicates.Skip(1).Aggregate(body, Expression.OrElse);
  65.             return Expression.Lambda<Func<T, bool>>(body, lbdSelector);
  66.         }
  67.  
  68.         private static IEnumerable<MethodCallExpression> PredicateContainsBuilder(Expression lbdSelector, PropertyInfo prop, Expression query)
  69.         {
  70.  
  71.             if (prop.PropertyType.IsClass)
  72.                 return new List<MethodCallExpression> { Expression.Call(Expression.Call(Expression.Property(lbdSelector, prop), ToStringMethod), StringContainsMethod, query) };
  73.  
  74.             var properties = prop.PropertyType.GetProperties();
  75.             return properties.Select(p => Expression.Call(Expression.Call(Expression.Property(lbdSelector, p), ToStringMethod), StringContainsMethod, query)).ToList();
  76.         }
  77.     }
  78.    
  79. private IQueryable SearchFilter(string param,string q,string filter,string value,string sort,string dir)
  80.     {
  81.         var emailMessage = db.EmailAflAwmMessage
  82.                             .Where(ExpressionBuilder.BuildFilterPredicate<EmailMessage>(q))
  83.                             .OrderBy(ExpressionBuilder.Selector<EmailMessage>(sort))
  84.                             .Select(m=> new{m.subject,m.msg_date});        
  85.     return emailmessage;
  86.     }
  87.    
  88. private IQueryable SortXpr(IQueryable<EmailAflAwmMessageDM> email ,string sort,string dir) {
  89.  
  90.     if (sort.Contains("to"))
  91.     {
  92.         if (dir.Contains("asc"))
  93.         {
  94.             return email.OrderBy(e => e.to_msg);
  95.         }
  96.         else
  97.         {
  98.             return email.OrderByDescending(e => e.to_msg);
  99.         }
  100.     }
  101.     else if (sort.Contains("from"))
  102.     {
  103.         if (dir.Contains("asc"))
  104.         {
  105.             return email.OrderBy(e => e.from_msg);
  106.         }
  107.         else
  108.         {
  109.             return email.OrderByDescending(e => e.from_msg);
  110.         }
  111.     }
  112.     else if (sort.Contains("subject"))
  113.     {
  114.         if (dir.Contains("asc"))
  115.         {
  116.             return email.OrderBy(e => e.subject);
  117.         }
  118.         else
  119.         {
  120.             return email.OrderByDescending(e => e.subject);
  121.         }
  122.     }
  123.     else
  124.     {
  125.         if (dir.Contains("asc"))
  126.         {
  127.             return email.OrderBy(e => e.msg_date);
  128.         }
  129.         else
  130.         {
  131.             return email.OrderByDescending(e => e.msg_date);
  132.         }
  133.     }
  134.  
  135. }
  136.    
  137. private Expression<Func<EmailAflAwmMessageDM, bool>>    FiltertXpr(string filter, string value)
  138. {
  139.     if (filter == "attachments")
  140.         return e => e.attachments == value;
  141.     else if (filter == "flagged")
  142.         return e => e.flagged == value;
  143.     else
  144.         return e => e.seen == value;
  145. }
  146.    
  147. private Expression<Func<EmailAflAwmMessageDM, bool>> SearchXpr(string param, string q)
  148. {
  149.     if (param == "to")
  150.         return e => e.to_msg.Contains(q);
  151.     else if (param == "from")
  152.         return e => e.from_msg.Contains(q);
  153.     else if (param == "cc")
  154.         return e => e.cc_msg.Contains(q);
  155.     else if (param == "bcc")
  156.         return e => e.bcc_msg.Contains(q);
  157.     else if (param == "subject")
  158.         return e => e.subject.Contains(q);
  159.     else
  160.         return e => e.body_text.Contains(q);
  161. }
  162.    
  163. private IQueryable SearchFilterCondition(string param,string q
  164.     ,string filter,string value,string sort,string dir)
  165.  {
  166.    var searchXpr = SearchXpr(param, q);
  167.    var filterXpr = FiltertXpr(filter, value);
  168.    IQueryable<EmailAflAwmMessageDM>
  169.    EmailAflAwmMessagejc = db.EmailAflAwmMessage.Where(filterXpr).Where(searchXpr);
  170.    return SortXpr(EmailAflAwmMessagejc, sort, dir);
  171.  }
RAW Paste Data
Top