I posted over at Codeplex but have not gotten a response in weeks.
How do you take the results from an Umbraco Examine search and put them into a datatable? It seems the IEnumerable collection is non standard? I tried some sample code that uses reflection but it fails. I then tried to manually add the rows to the datatable, but cannot figure out via trial and error how to pull the values out. I see lots of references online to GetValue but according to VS, that does not exist.
Think your ToDataTable method didn't work. Don't know why you did use Generics, My guess is that this code was copy pasted from the web? I modified it to use with the searchresults and now it should work. Only change the fieldalias for first and lastname with the real aliasses from Examine.
I finally came up with something similar, but I like yours, much cleaner. Below is my final code, but still maybe not quite right. I don't see how to create the new instance of SearchResults (commented out).
using Examine;
using Examine.LuceneEngine.SearchCriteria;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SBA.AZBar.UserControls
{
publicpartialclassMemberFinderResults_test : System.Web.UI.UserControl
{
///<summary>/// The terms being searched on///</summary>protectedstring SearchName { get; privateset; }
protectedstring SearchKeyword { get; privateset; }
///<summary>/// The search results list///</summary>protectedISearchResults SearchResults { get; privateset; }
public MemberFinderResults_test()
{
SearchName = string.Empty;
SearchKeyword = string.Empty;
//SearchResults = new List<SearchResult>();
}
protectedvoid Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["name"] != null && Regex.IsMatch(Request.QueryString["name"], @"^[a-zA-Z'.\s]{1,40}$"))
SearchName = Request.QueryString["name"];
if (Request.QueryString["keyword"] != null && Regex.IsMatch(Request.QueryString["keyword"], @"^[a-zA-Z'.\s]{1,40}$"))
SearchKeyword = Request.QueryString["keyword"];
if (string.IsNullOrEmpty(SearchName) && string.IsNullOrEmpty(SearchKeyword)) return;
var criteria = ExamineManager.Instance
.SearchProviderCollection["MemberFinderSearcher"]
.CreateSearchCriteria();
if (string.IsNullOrEmpty(SearchName))
{
var filter = criteria
.GroupedOr(newstring[] { "city", "state", "zipCode", "lawSchool", "areasOfPractice", "specialties", "sections", "languages", "jurisdictions", "extBio" }, SearchKeyword.Escape())
.Compile();
SearchResults = ExamineManager.Instance.SearchProviderCollection["MemberFinderSearcher"].Search(filter);
}
elseif (string.IsNullOrEmpty(SearchKeyword))
{
var filter = criteria
.Field("name", SearchName.Escape())
.Compile();
SearchResults = ExamineManager.Instance.SearchProviderCollection["MemberFinderSearcher"].Search(filter);
}
else
{
var filter = criteria
.Field("name", SearchName.Escape())
.And()
.GroupedOr(newstring[] { "city", "state", "zipCode", "lawSchool", "areasOfPractice", "specialties", "sections", "languages", "jurisdictions", "extBio" }, SearchKeyword.Escape())
.Compile();
SearchResults = ExamineManager.Instance.SearchProviderCollection["MemberFinderSearcher"].Search(filter);
}
DataTable dt = ToDataTable(SearchResults);
SearchResultListing.DataSource = dt;
SearchResultListing.DataBind();
}
publicstaticDataTable ToDataTable(ISearchResults result)
{
var tableToReturn = newDataTable();
tableToReturn.Columns.Add("Member_ID", typeof(String));
//tableToReturn.PrimaryKey = new DataColumn[] { tableToReturn.Columns["memberID"] };
tableToReturn.Columns.Add("name_first", typeof(String));
tableToReturn.Columns.Add("name_middle", typeof(String));
tableToReturn.Columns.Add("name_last", typeof(String));
tableToReturn.Columns.Add("name_suffix", typeof(String));
tableToReturn.Columns.Add("Area_of_Practice", typeof(String));
tableToReturn.Columns.Add("city", typeof(String));
//populate rowsforeach (var row in result)
{
DataRow newRow;
newRow = tableToReturn.NewRow();
newRow["Member_ID"] = row.Fields["memberId"];
newRow["name_first"] =row.Fields["name_first"];
newRow["name_middle"] = row.Fields["name_middle"];
newRow["name_last"] = row.Fields["name_last"];
newRow["name_suffix"] = row.Fields["name_suffix"];
newRow["Area_of_Practice"] = row.Fields["areasOfPractice"];
newRow["city"] = row.Fields["city"];
tableToReturn.Rows.Add(newRow);
}
return tableToReturn;
}
}
}
Umbraco Examine Search Results to Datatable
I posted over at Codeplex but have not gotten a response in weeks.
How do you take the results from an Umbraco Examine search and put them into a datatable? It seems the IEnumerable collection is non standard? I tried some sample code that uses reflection but it fails. I then tried to manually add the rows to the datatable, but cannot figure out via trial and error how to pull the values out. I see lots of references online to GetValue but according to VS, that does not exist.
Here is a snipped of what I have so far:
Connie,
Not sure why you want convert result set to datatable as the searchresults collection is IEnumerable and should bind to datacontrol?
Regards
Ismail
I need to plug into some preexisting code that paginates and displays search results. Don't have the time right now to rewrite how it works.
Ok, to ask this another way...
I can display a field from the search results like this:
<%# ((Examine.SearchResult)Container.DataItem).Fields["name_first"]%>
but how do I access that value in my code behind?
Anyone? Still cannot get Examine search results loaded into a datatable. Using reflection causes an error:
A column named 'Item' already belongs to this DataTable.
Hi,
Think your ToDataTable method didn't work. Don't know why you did use Generics, My guess is that this code was copy pasted from the web? I modified it to use with the searchresults and now it should work. Only change the fieldalias for first and lastname with the real aliasses from Examine.
Richard,
I finally came up with something similar, but I like yours, much cleaner. Below is my final code, but still maybe not quite right. I don't see how to create the new instance of SearchResults (commented out).
is working on a reply...