0

I was trying to perform a search operation. Somehow I've managed to implement the search operation for a column. Now I want to Search through multiple columns. for an example if my table contains the name, number, email, gender. and if I search for word M and my name column contains a field as Mandy and if my email column contains as [email protected] I want them both to be displayed.

How would I do this?

view

<td>@Html.TextBoxFor(model => model.name, new { @placeholder = "search by name" })</td>
            <td><input type="submit" value="search" class="btn-success" /></td>

MVC controller:

[HttpPost]
public ActionResult Index(Customer cvm)
{
    CustomerClient cc = new CustomerClient();
    ViewBag.listCustomers = cc.search(cvm.name);
    return View();
}

client Class:

public IEnumerable<Customer> search(int name)
{
    try
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(Base_URL);
        client.DefaultRequestHeaders.Accept.Add(new 
            MediaTypeWithQualityHeaderValue("application/json"));
        var result = client.GetAsync(string.Format("customers/search/{0}", name)).Result;
        if (result.IsSuccessStatusCode)
            return result.Content.ReadAsAsync<IEnumerable<Customer>>().Result;
        return null;
    }
    catch
    {
        return null;
    }
}

API Controller:

[HttpGet]
[Route("search/{name}")]
public HttpResponseMessage search(int name)
{
    try
    {
        var httpresponsemessage = new HttpResponseMessage();

        httpresponsemessage.Content = new StringContent(
            JsonConvert.SerializeObject(db.customers.Where(
            p => p.name.Contains(name)).ToList()));

        httpresponsemessage.Content.Headers.ContentType = new
            MediaTypeHeaderValue("application/json");
        return httpresponsemessage;
    }
    catch
    {
        return null;
    }
}
3
  • Can you try to add OR condition where you have managed to implement the search operation for name column? Commented Oct 11, 2018 at 13:00
  • Post your code! Is it stored procedure or anything that you have tried! Commented Oct 11, 2018 at 13:02
  • @PrashantPimpale i have posted the code. Commented Oct 12, 2018 at 5:01

1 Answer 1

4

As assume, if you are using Query then try below one:

DECLARE @search NVARCHAR(MAX)

SELECT * 
FROM [TABLE_NAME]
WHERE name LIKE '%' + @search + '%' OR number LIKE '%' + @search + '%' OR
      email LIKE '%' + @search + '%' OR gender LIKE '%' + @search + '%'

If you are using EF then try below one,

db.customers.Where(p => p.name.Contains(name) || p.number.Contains(name)
                    || p.email.Contains(name) || p.gender.Contains(name)).ToList();
Sign up to request clarification or add additional context in comments.

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.