Selman Erhan ekici Selman Erhan ekici - 5 months ago 7
SQL Question

Search in all tables

My problem is I dont know how to search all tables and search in all fields of the tables. I have no idea.
Just one textbox such as YouTube, results will be written in the source.
Can someone give me an idea? I would be very pleased.
I have to do it on the Asp.net MVC

Answer

Search using the same string and when searching through

var resultFromFirstTable = from t1 in Table1 select t1;

if (!String.IsNullOrEmpty(SearchString))
{
    resultFromFirstTable = Table1.Where(r => r.Name.Contains(SearchString));
}

var resultFromSecondTable = from t2 in Table2 select t2;

if (!String.IsNullOrEmpty(SearchString))
{
    resultFromSecondTable = Table2.Where(r => r.Name.Contains(SearchString));
}
if (!String.IsNullOrEmpty(SearchString))
{
    resultFromSecondTable = Table2.Where(r => r.SecondColumn.Contains(SearchString));
}

return View(resultFromFirstTable.Union(resultFromSecondTable ).ToList());

You can also do it by using the DbContext in MVC 5, I donĀ“t quite remember if it could be done like this in MVC 3 but you can try it out:

var resultFromFirstTable = DbContext.Table1.ToList();