Shubham Singh
Shubham Singh

Reputation: 75

Implementing search in mvc 4

I have a textbox where user types the string he needs to search. If the user enters only a single word string, then I am able to retrieve the correct data from database but if the user enters a multi-word string then my code fails.

I am using EntityFramework to get the data.

Here is my code to get the data using a single word string.

public ActionResult SearchResult(string search)
{
   var j = objCon.Mobiles.Where(oh => oh.MobileName.Contains(search) || oh.Description.Contains(search));
   List<Mobiles> prod = new List<Mobiles>();
   foreach (var p in j)
   {
       Mobiles Mob = new Mobiles();
       Mob.Description = p.Description;
       Mob.ImgUrl = p.Url;
       Mob.MobileName = p.MobileName;
       Mob.Price = Convert.ToString(p.Price);
       Mob.SlNo = p.SlNo;
       prod.Add(Mob);
   }

   return View("~/Views/Product/Index.cshtml", prod);
}

I tried breaking the string into single word using split but could not get the correct data.

string str = null;
string[] strArr = null;
int count = 0;
str = //UserInput;
char[] splitchar = { ' ' };
strArr = str.Split(splitchar);

Upvotes: 1

Views: 96

Answers (2)

Amin Golmahalleh
Amin Golmahalleh

Reputation: 4216

I Wrote an Example to Solve your Problem. Hope That You will Be Benefited From The Code.

First Create Mobile Class:

 public class Mobile
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }
        }

Next Create Extension method To Check If there is Value:

public static bool ContainsAny(this string haystack, params string[] needles)
        {
            foreach (var needle in needles)
            {
                if (haystack.Contains(needle))    
                    return true;
            }

            return false;
        }  

Finally Create Main Body Along with Test Data:

using System;
using System.Collections.Generic;
using System.Linq;

namespace StackOverFlow
{
    static class Program
    {
        static void Main()
        {
            List<Mobile> mobiles = new List<Mobile>
            {
                new Mobile{Id = 1,Name = "samsung galaxy s3",Description = "model"},
                new Mobile{Id = 2,Name = "nokia N67",Description = "nokia n96 time"},
                new Mobile{Id = 3,Name = "iphone 5s",Description = "test"},
                new Mobile{Id = 4,Name = "samsung galaxy packet",Description = "this time"},
                new Mobile{Id = 5,Name = "iphone ipad",Description = "now"},
                new Mobile{Id = 6,Name = "glx c5",Description = "time"},
            };

            string[] search = "galaxy time 5s".Split(' ');

            var result = mobiles.Where(c => c.Name.ContainsAny(search) ||
                                            c.Description.ContainsAny(search)).ToList();

            foreach (var item in result)
            {
                Console.WriteLine(item.Id + "-" + item.Name + "-" + item.Description);
            }

            Console.ReadKey();
        }

Upvotes: 1

ChizT
ChizT

Reputation: 727

string str = null;
        string[] strArr = null;
        int count = 0;
        str = search;
        char[] splitchar = { ' ' };
        strArr = str.Split(splitchar);
        for (count = 0; count <= strArr.Length - 1; count++)
        {
            string i = strArr[count];
            var j = objCon.Mobiles.Where(oh => oh.MobileName.Contains(i) || oh.Description.Contains(i));
            //MessageBox.Show(strArr[count]); 
            foreach (var p in j)
            {
                Mobiles Mob = new Mobiles();
                Mob.Description = p.Description;
                Mob.ImgUrl = p.Url;
                Mob.MobileName = p.MobileName;
                Mob.Price = Convert.ToString(p.Price);
                Mob.SlNo = p.SlNo;
                prod.Add(Mob);
            }
        }

as I help you fix the problem - this is the final code

Upvotes: 1

Related Questions