2

I having trouble finding out how to handle multiple words when the user searches. An example: Search: "Blue box" it should be able to find: "One box is blue". How would I do this? This is basically how my controller looks like atm:

public ActionResult Index(string searchString)
{
    var posts = from s in _context.Posts
                   select s;

    var postIndexViewModel = new PostIndexViewModel();

    if (!String.IsNullOrEmpty(searchString))
    {
            posts = posts.Where(s => s.Title.Contains(searchString));
     }

    // More code here

    return View(postIndexViewModel);

}
Rockyy
  • 273
  • 4
  • 14
  • 1
    first use searchString.Split(' ') to split the searchstring into an array, then look here: http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array – Stephen Jul 03 '15 at 11:21
  • `split` the searchString using space. Then use `Array.Any()` as shown here - http://stackoverflow.com/questions/1757214/linq-entity-string-field-contains-any-of-an-array-of-strings – ramiramilu Jul 03 '15 at 11:22
  • Check this link http://stackoverflow.com/questions/11560921/linq-query-to-match-multiple-words – Bhavin Jul 03 '15 at 11:29

3 Answers3

3

Your problem is that you are doing the contains with a whole string. That means it must contain "Blue box" in that order.

This is what you need to do:

var strings = searchString.Split(' ');
var finalPosts = new List<string>();
if (!String.IsNullOrEmpty(searchString))
{
    foreach (var splitString in strings)
    {
        finalPosts.Add(posts.FirstOrDefault(s => s.Title.Contains(splitString)));
    }     
}

The finalPosts list then contains your results.

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • This looks correct. What would I do to fix the problem that `cannot convert from 'System.Linq.IQueryable' to 'string'` on the `finalPosts.Add()` line? – Rockyy Jul 03 '15 at 11:50
  • Apologies, I was unsure what your type was of `post` Just change the `var finalPosts = new List();` to: `var finalPosts = new List();` – Jamie Rees Jul 03 '15 at 11:51
  • :) I tried that aswell. Got `cannot convert from 'System.Linq.IQueryable' to 'WebApplication3.Models.Post'` – Rockyy Jul 03 '15 at 11:53
  • 1
    Ah `posts` is a `List` already. Use this `finalPosts.Add(posts.FirstOrDefault(s => s.Title.Contains(splitString)));` – Jamie Rees Jul 03 '15 at 12:00
  • Thank you! btw is there a way to not use the finalPosts since posts is already a list? – Rockyy Jul 03 '15 at 12:27
  • 1
    @Rockyy Of course, I just created a new `List` since I did not know what you wanted to do with the results. Using the List you now have all of the search results in one place. – Jamie Rees Jul 03 '15 at 12:40
1

One way I can think of is to search term by term by splitting search string passed in.

public ActionResult Index(string searchString)
{
    var posts = from s in _context.Posts
                   select s;

    var postIndexViewModel = new PostIndexViewModel();

    if (!String.IsNullOrEmpty(searchString))
    {
        var terms = searchString.Trim().Split(' ');
        posts = posts.Where(s => terms.Any(terms.Contains));
     }

    // More code here

    return View(postIndexViewModel);

}
Yang Zhang
  • 4,540
  • 4
  • 37
  • 34
0

This works perfectly well for me. See the code snippet and the screenshot below:

string[] SplitValue = StaffSearch.Split(",");

List<HistorypodScans> query = new List<HistorypodScans>();

foreach (var word in SplitValue)
{
      var staffquery = _db.HistorypodScans.Where(x => x.Waybillnumber.Contains(word) || x.AWBNO.Contains(word)).ToList();
      query.AddRange(staffquery);

}

return View(query);


[enter image description here][1]     

[1]: https://i.stack.imgur.com/TRmMW.jpg Multiple Words Search Result