Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Connie DeCinko 931 posts 1160 karma points
    Jul 13, 2011 @ 17:23
    Connie DeCinko
    0

    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:

    ...
    DataTable dt = ToDataTable(SearchResults); SearchResultListing.DataSource = dt; SearchResultListing.DataBind(); } public static DataTable ToDataTable<T>(IEnumerable<T> rows) { var tableToReturn = new DataTable(); tableToReturn.Columns.Add("memberID"typeof(String)); //tableToReturn.PrimaryKey = new DataColumn[] { tableToReturn.Columns["memberID"] }; tableToReturn.Columns.Add("name_first"typeof(String)); tableToReturn.Columns.Add("name_last"typeof(String)); //populate rows foreach (var row in rows) { DataRow newRow; newRow = tableToReturn.NewRow(); newRow["memberID"] = (row)Container.DataItem..ToString(); newRow["name_first"] = row.ToString(); newRow["name_last"] = row.ToString(); tableToReturn.Rows.Add(newRow); } return tableToReturn; }
  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jul 13, 2011 @ 18:08
    Ismail Mayat
    0

    Connie,

    Not sure why you want convert result set to datatable as the searchresults collection is IEnumerable and should bind to datacontrol?

    Regards

    Ismail

  • Connie DeCinko 931 posts 1160 karma points
    Jul 13, 2011 @ 18:10
    Connie DeCinko
    0

    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.

     

  • Connie DeCinko 931 posts 1160 karma points
    Jul 14, 2011 @ 23:51
    Connie DeCinko
    0

    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?

     

  • Connie DeCinko 931 posts 1160 karma points
    Jul 25, 2011 @ 20:41
    Connie DeCinko
    0

    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.

  • Richard Soeteman 4045 posts 12898 karma points MVP 2x
    Jul 26, 2011 @ 06:38
    Richard Soeteman
    0

    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.

      public static DataTable ToDataTable(ISearchResults result)
                    {
                            var tableToReturn = new DataTable();
                            tableToReturn.Columns.Add("memberID"typeof(String));
                            //tableToReturn.PrimaryKey = new DataColumn[] { tableToReturn.Columns["memberID"] };
                            tableToReturn.Columns.Add("name_first"typeof(String));
                            tableToReturn.Columns.Add("name_last"typeof(String));
     
                            //populate rows
                            foreach (var row in result)
                            {
                                    DataRow newRow;
                                    newRow = tableToReturn.NewRow();
     
                                    newRow["memberID"] = row.Id.ToString();
                                    newRow["name_first"] = row.Fields["your examine column"];
                                    newRow["name_last"] = row.Fields["your examine column"];
                                    tableToReturn.Rows.Add(newRow);
     
                            }
                            return tableToReturn;
                    }
  • Connie DeCinko 931 posts 1160 karma points
    Jul 26, 2011 @ 18:00
    Connie DeCinko
    0

    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).

    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
    {
        public partial class MemberFinderResults_test : System.Web.UI.UserControl
        {
            /// <summary>
            /// The terms being searched on
            /// </summary>
            protected string SearchName { getprivate set; }
            protected string SearchKeyword { getprivate set; }
    
            /// <summary>
            /// The search results list
            /// </summary>
            protected ISearchResults SearchResults { getprivate set; }
    
            public MemberFinderResults_test()
            {
                SearchName = string.Empty;
                SearchKeyword = string.Empty;
                //SearchResults = new List<SearchResult>();
            }
    
            protected void 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(new string[] { "city""state""zipCode""lawSchool""areasOfPractice""specialties""sections""languages""jurisdictions""extBio" }, SearchKeyword.Escape())
                        .Compile();
                    SearchResults = ExamineManager.Instance.SearchProviderCollection["MemberFinderSearcher"].Search(filter);
                }
                else if (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(new string[] { "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();
    
            }
    
    
            public static DataTable ToDataTable(ISearchResults result)
            {
                var tableToReturn = new DataTable();
                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 rows
                foreach (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;
            }
    
        }
    }
    
    
Please Sign in or register to post replies

Write your reply to:

Draft