Substring search in RavenDB

后端 未结 4 1825
广开言路
广开言路 2021-02-08 10:42

I have a set of objects of type Idea

public class Idea
{
    public string Title { get; set; }
    public string Body { get; set; }
}
4条回答
  •  粉色の甜心
    2021-02-08 11:30

    I managed to do this in memory with the following code:

    public virtual ActionResult Search(string term)
    {
        var clientNames = from customer in DocumentSession.Query()
                            select new { label = customer.FullName };
    
        var results = from name in clientNames.ToArray()
                        where name.label.Contains(term,
                                                 StringComparison.CurrentCultureIgnoreCase)
                        select name;
    
        return Json(results.ToArray(), JsonRequestBehavior.AllowGet);
    }
    

    This saved me the trouble of going RavenDB way of searching for strings with Contains method as described by Daniel Lang's post.

    The Contains extension method is this:

    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
         return source.IndexOf(toCheck, comp) >= 0;
    }
    

提交回复
热议问题