Process Query Results |
Scroll |
The code sample below illustrates various ways to process and iterate an already existing TheQueryResult. Please see "Simple Query" and/or "Query in One Category" for more information on specifying and executing queries.
Visual Basic Imports Therefore.API '...
Public Shared Sub ProcessQueryResult( _ ByVal result As TheQueryResult) ' Iterate through search result using nested for-loops Dim i As Integer For i = 0 To result.Count - 1 Step i + 1 Console.WriteLine(result(i).ToString()) Dim j As Integer For j = 0 To result(i).Count - 1 Step j + 1 ' Access the j-th field in the i-th row If Not result(i)(j) Is Nothing Then Console.WriteLine(result(i)(j).ToString()) End If Next Next
' Iterate through search result using for-each constructs ' Type-specific handling of field values Dim row As TheQueryResultRow For Each row In result Console.WriteLine(row.ToString()) Dim obj As Object For Each obj In row If TypeOf obj Is DBNull Then Console.WriteLine("null") ElseIf TypeOf obj Is Integer Then Dim n As Integer = CType(obj, Integer) Console.WriteLine("{0} * {0} = {1}", n, n * n) ElseIf TypeOf obj Is Double Then Dim d As Double = CType(obj, Double) Console.WriteLine("{0} / 2 = {1}", d, d / 2) ElseIf TypeOf obj Is String Then Dim s As String = CType(obj, String) Console.WriteLine("{0:} is a string", s) ElseIf TypeOf obj Is DateTime Then Dim dt As DateTime = CType(obj, DateTime) Dim ts As TimeSpan = DateTime.Now - dt Console.WriteLine(ts.TotalDays.ToString() + "days ago.") Else Console.WriteLine("A " + obj.GetType().ToString()) End If Next Next End Sub C# using Therefore.API; //...
public static void ProcessQueryResult ( TheQueryResult result) { // Iterate through search result using nested for-loops for (int i = 0; i < result.Count; i++) { Console.WriteLine(result[i].ToString()); for (int j = 0; j < result[i].Count; j++) { // Access the j-th field in the i-th row if (result[i][j] != null) Console.WriteLine(result[i][j].ToString()); } }
// Iterate through search result using the for-each constructs // Type-specific handling of field values foreach (TheQueryResultRow row in result) { Console.WriteLine(row.ToString()); foreach (object obj in row) { if (obj is DBNull) { Console.WriteLine("null"); } else if (obj is int) { int i = (int)obj; Console.WriteLine("{0} * {0} = {1}", i, i * i); } else if (obj is double) { double d = (double)obj; Console.WriteLine("{0} / 2 = {1}", d, d / 2); } else if (obj is string) { string s = (string)obj; Console.WriteLine("\"{0}\" is a string", s); } else if (obj is DateTime) { DateTime dt = (DateTime)obj; TimeSpan ts = DateTime.Now - dt; Console.WriteLine(ts.TotalDays + "days ago."); } else { Console.WriteLine("A " + obj.GetType().ToString()); } } } }
|