Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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"]));
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
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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"]));
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
is working on a reply...