Copied to clipboard

Flag this post as spam?

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


  • Mike Taylor 155 posts 353 karma points
    Aug 25, 2011 @ 10:45
    Mike Taylor
    0

    Examine search - OrderByDescending: can I fallback to a default value if a particular field doesn't exist?

    I'm doing an Examine search using a raw string like this:

    IEnumerable<SearchResult> SearchResults = searchProvider.Search(searchCriteria.RawQuery(
                    examineQuery
                    )).OrderByDescending(r => r.Fields["publishedDate"]);

    But this fails if any of the items in the index don't have a "publishedDate" field.

    Is it possible to do something like this?

     

    IEnumerable<SearchResult> SearchResults = searchProvider.Search(searchCriteria.RawQuery(
                    examineQuery
                    )).OrderByDescending(r => (r.Fields["publishedDate"] == "" ? {some default value} : r.Fields["publishedDate"]));

    In other words, can OrderByDescending sort on a field if it exists, but fall back to a default value if it doesn't?
    Cheers,
    Mike

     

  • Mike Taylor 155 posts 353 karma points
    Aug 26, 2011 @ 10:54
    Mike Taylor
    0

    Hmm. It looks like a custom IComparer might be the way to go:

    Comparer comparer = new MyComparer();
    IEnumerable<SearchResult> SearchResults = searchProvider.Search(searchCriteria.RawQuery(

                    examineQuery
                    )).OrderByDescending(r => r.Fields["publishedDate"], comparer);

    But I can't get this working either - here's my current comparer class:

        public class MyComparer : IComparer<object>
        {
            public int Compare(object x, object y)
            {
                DateTime oldestDate = DateTime.MinValue;
                string oldestDateString = oldestDate.Year.ToString() + "-" + oldestDate.Month.ToString() + "-" + oldestDate.Day.ToString();

                string valueX = (x == null ? oldestDateString : x.ToString());
                string valueY = (y == null ? oldestDateString : y.ToString());

                return valueX.CompareTo(valueY);
            }
        } 

    But this is still throwing errors. If one of my index items doesn't have a "publishedDate" field, I want it to treat the date as equivalent to DateTime.MinValue.

    Help....

    M

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies