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);

}
3

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

This looks correct. What would I do to fix the problem that cannot convert from 'System.Linq.IQueryable<WebApplication3.Models.Post>' to 'string' on the finalPosts.Add() line?
Apologies, I was unsure what your type was of post Just change the var finalPosts = new List<string>(); to: var finalPosts = new List<Post>();
:) I tried that aswell. Got cannot convert from 'System.Linq.IQueryable<WebApplication3.Models.Post>' to 'WebApplication3.Models.Post'
Ah posts is a List<Post> already. Use this finalPosts.Add(posts.FirstOrDefault(s => s.Title.Contains(splitString)));
@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.
|
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);

}

Comments

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.sstatic.net/TRmMW.jpg Multiple Words Search Result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.