Advertisement
Guest User

Untitled

a guest
Apr 29th, 2013
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. using System.Windows.Forms.DataVisualization.Charting;
  11. using System.Linq.Expressions;
  12.  
  13. namespace Test_2
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         private List<Expression<Func<Personnel, bool>>> personnelFilter = new List<Expression<Func<Personnel,bool>>>();
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.  
  23.             AddFilter(a => a.FirstName == "Test");
  24.             AddFilter(a => a.FirstName == "More");
  25.  
  26.             // Or adding something stupid like:
  27.             AddFilter(a => a.Id == 1);
  28.         }
  29.  
  30.         public void AddFilter(Expression<Func<Personnel, bool>> filter)
  31.         {
  32.             this.personnelFilter.Add(filter);
  33.         }
  34.  
  35.         public static Expression<Func<T, bool>> Combine_Or<T>(Expression<Func<T, bool>> expr1,
  36.                                                     Expression<Func<T, bool>> expr2)
  37.         {
  38.             var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
  39.             return Expression.Lambda<Func<T, bool>>
  40.                   (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
  41.         }
  42.  
  43.         private Expression<Func<Personnel, bool>> JoinFilter()
  44.         {
  45.             var result = personnelFilter[0];
  46.             for (int i = 1; i < personnelFilter.Count; i++)
  47.                 result = Combine_Or<Personnel>(result, personnelFilter[i]);
  48.  
  49.             return result;
  50.         }
  51.  
  52.         private void button1_Click(object sender, EventArgs e)
  53.         {
  54.             List<Personnel> personnel = new List<Personnel> {
  55.                 new Personnel { Id=3, FirstName="walter" },
  56.                 new Personnel { Id=1, FirstName="More" },
  57.                 new Personnel { Id=1, FirstName="More" },
  58.                 new Personnel { Id=1, FirstName="john" },
  59.                 new Personnel { Id=2, FirstName="michael" },
  60.                 new Personnel { Id=2, FirstName="Test" },
  61.                 new Personnel { Id=1, FirstName="Raj" }
  62.             };
  63.  
  64.             Func<Personnel, bool> filter = JoinFilter().Compile();
  65.             var filtered = personnel.Where(filter);
  66.  
  67.             foreach (var item in filtered)
  68.                 System.Console.WriteLine(item.FirstName);
  69.  
  70.             /* Prints:
  71.                 More
  72.                 More
  73.                 john
  74.                 Test
  75.                 Raj
  76.              */
  77.         }      
  78.     }
  79.  
  80.     public class Personnel
  81.     {
  82.         public int Id { get; set; }
  83.         public string FirstName { get; set; }
  84.         public string LastName { get; set; }
  85.         public int OrganizationalUnitId { get; set; }
  86.         public int RoleId { get; set; }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement