locked
Distinct department names RRS feed

  • Question

  • Hi,

    I need to get distinct department values where scope is people.
    I tried with Sharepoint full text query but this does not seem to return distinct values. Therefore, the only thing i can do is to get distinct values from the datatable.
    But this approach slows down the application when there are huge number of people belonging to a specific department as the department name repeats so many times as the number of people belonging to that department.

    What can be the alternative?

    Thanks,
    Wednesday, September 23, 2009 3:28 PM

Answers

  • Hi,     

     

    As your information, you need to something like “Distinct” in search result’s data-table.

    Perhaps it is difficult to control over this, in your situation, I would recommend use Linq and Distinct method. 


    A sample for this:

    --------------------------------------------------------------------------------------------------

    private void DisplayDistinct()

    {

       try

       {

           DataTable dt=new DataTable("SearchResult");

           FullTextSqlQuery sqlQuery = new FullTextSqlQuery(new SPSite(SitePath));

           ResultTableCollection results = sqlQuery.Execute();

           ResultTable relResults = results[ResultType.RelevantResults];

           dt.Load(relResults);

           //Use LINQ to get the unique values from the result.

           DepartRowComparer dCompare=new DepartRowComparer();
               IEnumerable<DataRow> distValues = dt.AsEnumerable().Distinct(dCompare);

           foreach (DataRow dr in distValues)

           {

              //Here display the distinct-value

           } 

        }   

                  

        catch (Exception ex)

        {               

             throw(ex);

        }

    }

    public class DepartRowComparer : IEqualityComparer<DataRow>

     {

        public bool Equals(DataRow x, DataRow y)
        {
                return (x.Field<string>("Title") == y.Field<string>("Title"));
        }

    public int GetHashCode(DataRow obj)
        {
                return obj.ToString().GetHashCode();
        }

    }
    --------------------------------------------------------------------------------------------------

     

    Hope this is helpful.

     

    Best Regards,

    -Aaron

     

    • Marked as answer by Chengyi Wu Friday, October 2, 2009 8:50 AM
    Tuesday, September 29, 2009 10:51 AM
  • Agree with Aaron, LINQ is the best way to filter the datatable.  There's a similar thread here http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/1a3afda8-a9c0-4bff-8839-955d9a162dff/
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Friday, October 2, 2009 8:50 AM
    Wednesday, September 30, 2009 11:11 AM

All replies

  • Hi,     

     

    As your information, you need to something like “Distinct” in search result’s data-table.

    Perhaps it is difficult to control over this, in your situation, I would recommend use Linq and Distinct method. 


    A sample for this:

    --------------------------------------------------------------------------------------------------

    private void DisplayDistinct()

    {

       try

       {

           DataTable dt=new DataTable("SearchResult");

           FullTextSqlQuery sqlQuery = new FullTextSqlQuery(new SPSite(SitePath));

           ResultTableCollection results = sqlQuery.Execute();

           ResultTable relResults = results[ResultType.RelevantResults];

           dt.Load(relResults);

           //Use LINQ to get the unique values from the result.

           DepartRowComparer dCompare=new DepartRowComparer();
               IEnumerable<DataRow> distValues = dt.AsEnumerable().Distinct(dCompare);

           foreach (DataRow dr in distValues)

           {

              //Here display the distinct-value

           } 

        }   

                  

        catch (Exception ex)

        {               

             throw(ex);

        }

    }

    public class DepartRowComparer : IEqualityComparer<DataRow>

     {

        public bool Equals(DataRow x, DataRow y)
        {
                return (x.Field<string>("Title") == y.Field<string>("Title"));
        }

    public int GetHashCode(DataRow obj)
        {
                return obj.ToString().GetHashCode();
        }

    }
    --------------------------------------------------------------------------------------------------

     

    Hope this is helpful.

     

    Best Regards,

    -Aaron

     

    • Marked as answer by Chengyi Wu Friday, October 2, 2009 8:50 AM
    Tuesday, September 29, 2009 10:51 AM
  • hi,
      try this

    DataTable dt = dvwDataView.ToTable(true, string[]);

    string[] contains the columns which u need. U have to create string[] first

    true returns the distinct values
    Wednesday, September 30, 2009 10:59 AM
  • http://geek.hubkey.com/2008/12/get-distinct-lookup-values.html
     
    check this link for reference
    Wednesday, September 30, 2009 11:02 AM
  • Agree with Aaron, LINQ is the best way to filter the datatable.  There's a similar thread here http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/1a3afda8-a9c0-4bff-8839-955d9a162dff/
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by Chengyi Wu Friday, October 2, 2009 8:50 AM
    Wednesday, September 30, 2009 11:11 AM