Guest User

Untitled

a guest
Aug 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. LINQ Expression in dataset
  2. Expression<Func<int, bool>> ageexpr; // = creteriaattributeIDtest => creteriaattributeIDtest < Convert.ToInt32(rightval1);
  3.  
  4. ParameterExpression numparam = Expression.Parameter(typeof(int), "age");
  5. ConstantExpression criteriaValue1 = Expression.Constant(Convert.ToInt32(rightval1), typeof(int));
  6. BinaryExpression comparison1 = Expression.LessThan(numparam, criteriaValue1);
  7.  
  8. ageexpr = Expression.Lambda<Func<int, bool>>(
  9. comparison1,
  10. new ParameterExpression[] { numparam });
  11.  
  12. Func<int, bool> resultage = ageexpr.Compile();
  13.  
  14. // Invoking the delegate and writing the result to the console.
  15.  
  16. bool firstrule = resultage(14);
  17.  
  18. Console.WriteLine("1st Rule" + firstrule);
  19.  
  20. // DataView res1 = dt1.AsEnumerable().Where(resultage(Convert.ToInt32(rightval1))).AsDataView();
  21.  
  22.  
  23. Expression<Func<string, bool>> genexpr;
  24.  
  25. ParameterExpression genparam = Expression.Parameter(typeof(string), "gender");
  26. ConstantExpression criteriaValue2 = Expression.Constant("M", typeof(string));
  27. BinaryExpression comparison2 = Expression.Equal(genparam, criteriaValue2);
  28.  
  29. genexpr = Expression.Lambda<Func<string, bool>>(
  30. comparison2,
  31. new ParameterExpression[] { genparam });
  32.  
  33. Func<string, bool> resultgen = genexpr.Compile();
  34.  
  35. bool secondrule = resultgen("M");
  36.  
  37. // Invoking the delegate and writing the result to the console.
  38. Console.WriteLine("2nd Rule" + secondrule);
  39.  
  40.  
  41. Expression finexpr = Expression.AndAlso(Expression.Constant(firstrule), Expression.Constant(secondrule));
  42. Console.WriteLine(Expression.Lambda<Func<bool>>(finexpr).Compile()());
  43.  
  44. static void Main(string[] args)
  45. {
  46. var minimunAge = 10;
  47.  
  48. var t = new DataTable();
  49. t.Columns.Add("age", typeof(Int32));
  50. t.Columns.Add("gender", typeof(string));
  51. t.Columns.Add("name", typeof(string));
  52.  
  53. t.Rows.Add(20, "M", "Steve");
  54. t.Rows.Add(5, "M", "John");
  55. t.Rows.Add(32, "F", "Mary");
  56.  
  57. var view = t.AsEnumerable().Where(r => r.Field<Int32>("age") > minimunAge && r.Field<string>("gender") == "M").AsDataView();
  58. }
  59.  
  60. static void Main(string[] args)
  61. {
  62. var t = new DataTable();
  63. t.Columns.Add("age", typeof(Int32));
  64. t.Columns.Add("gender", typeof(string));
  65. t.Columns.Add("name", typeof(string));
  66.  
  67. t.Rows.Add(20, "M", "Steve");
  68. t.Rows.Add(5, "M", "John");
  69. t.Rows.Add(32, "F", "Mary");
  70.  
  71. Expression<Func<int, bool>> ageexpr;
  72.  
  73. ParameterExpression numparam = Expression.Parameter(typeof(int), "age");
  74. ConstantExpression criteriaValue1 = Expression.Constant(Convert.ToInt32(10), typeof(int));
  75. BinaryExpression comparison1 = Expression.GreaterThan(numparam, criteriaValue1);
  76.  
  77. ageexpr = Expression.Lambda<Func<int, bool>>(
  78. comparison1,
  79. new ParameterExpression[] { numparam });
  80.  
  81. Func<int, bool> resultage = ageexpr.Compile();
  82.  
  83. // Invoking the delegate and writing the result to the console.
  84.  
  85. bool firstrule = resultage(14);
  86.  
  87. Console.WriteLine("1st Rule" + firstrule);
  88.  
  89. DataView res1 = t.AsEnumerable().Where(r=> resultage(r.Field<Int32>("age"))).AsDataView();
  90. }
Add Comment
Please, Sign In to add comment