Guest User

Untitled

a guest
Sep 9th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. ASP.NET Dynamic Linq Search
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Data;
  6.  
  7. namespace Test1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. DataTable table = new DataTable();
  14. table.Columns.Add("ID", typeof(int));
  15. table.Columns.Add("FIRSTNAME", typeof(string));
  16. table.Columns.Add("LASTNAME", typeof(string));
  17. table.Columns.Add("EMAIL", typeof(string));
  18.  
  19. // Here we add five DataRows.
  20. table.Rows.Add(1, "Chris", "Foo", "chris.foo@mail.com");
  21. table.Rows.Add(2, "Christoph", "Bar", "christoph.bar@mail.com");
  22. table.Rows.Add(3, "Michael", "FooBar", "michael.foobar@mail.com");
  23. table.Rows.Add(4, "Andreas", "BarFoo", "andreas.barfoo@mail.com");
  24. table.Rows.Add(5, "Carl", "Bar", "carl.bar@mail.com");
  25.  
  26. Console.WriteLine("//Query ID");
  27. var query1 = (from dr in table.AsEnumerable() where dr.Field<int>("ID") == 1 select dr).FirstOrDefault();
  28.  
  29. Console.WriteLine(query1.Field<int>("ID"));
  30.  
  31. Console.WriteLine("//Query Firstname");
  32. var query2 = (from dr in table.AsEnumerable() where dr.Field<string>("FIRSTNAME").StartsWith("C") select dr).ToList<System.Data.DataRow>();
  33.  
  34. foreach (var q in query2)
  35. {
  36. Console.WriteLine(q.Field<int>("ID"));
  37. }
  38.  
  39. Console.ReadLine();
  40. }
  41. }
  42. }
  43.  
  44. using System;
  45. using System.Collections.Generic;
  46. using System.Linq;
  47. using System.Data;
  48.  
  49. namespace Test1
  50. {
  51. class Program
  52. {
  53. static void Main(string[] args)
  54. {
  55. DataTable table = new DataTable();
  56. table.Columns.Add("ID", typeof(int));
  57. table.Columns.Add("FIRSTNAME", typeof(string));
  58. table.Columns.Add("LASTNAME", typeof(string));
  59. table.Columns.Add("EMAIL", typeof(string));
  60.  
  61. // Here we add five DataRows.
  62. table.Rows.Add(1, "Chris", "Foo", "chris.foo@mail.com");
  63. table.Rows.Add(2, "Christoph", "Bar", "christoph.bar@mail.com");
  64. table.Rows.Add(3, "Michael", "FooBar", "michael.foobar@mail.com");
  65. table.Rows.Add(4, "Andreas", "BarFoo", "andreas.barfoo@mail.com");
  66. table.Rows.Add(5, "Carl", "Bar", "carl.bar@mail.com");
  67.  
  68. Console.WriteLine("//Query ID");
  69. var query1 = (from dr in table.AsEnumerable() where dr.Field<int>("ID") == 1 select dr).FirstOrDefault();
  70.  
  71. Console.WriteLine(query1.Field<int>("ID"));
  72.  
  73. Console.WriteLine("//Query Firstname");
  74. var query2 = (from dr in table.AsEnumerable() where dr.Field<string>("FIRSTNAME").StartsWith("C") select dr).ToList<System.Data.DataRow>();
  75.  
  76. foreach (var q in query2)
  77. {
  78. Console.WriteLine(q.Field<int>("ID"));
  79. }
  80.  
  81. Console.ReadLine();
  82. }
  83. }
  84. }
  85.  
  86. //Query ID
  87. 1
  88. //Query Firstname
  89. 1
  90. 2
  91. 5
  92.  
  93. var query = (from u in results select u);
  94.  
  95. if (!string.IsNullOrEmpty(userRequest.searchData))
  96. {
  97. if (userRequest.searchBy == "LastName")
  98. {
  99. var likestr = userRequest.searchData.Trim();
  100. query = (from n in query where n.LastName.StartsWith(likestr) select n);
  101. }
  102. if (userRequest.searchBy == "UserId")
  103. {
  104. var userId = Convert.ToInt32(userRequest.searchData);
  105. query = (from n in query where n.UserId == userId select n);
  106. }
Add Comment
Please, Sign In to add comment