I have 3 tables in my database which have no relation with each other. what i want is to implement the search operation on my website. So that when a word is submit in search box the query go through all the tables and fetch the data wherever it find that word. I can fetch the data from single table.
public ActionResult Searchresult(string searchString)
{
var article = (from c in db.Tbl_Article select c );
article = article.Where(s => s.Article_Title.Contains(searchString));
var blog = (from c in db.Tbl_Blog select c );
blog = blog.Where(s => s.Blog_Title.Contains(searchString));
var history = (from c in db.Tbl_History select c);
history = history.Where(s => s.Title.Contains(searchString));
var result = article.Select(x => x.Article_Title).Union(blog.Select(x => x.Blog_Title)).Union(history.Select(x => x.Title)).ToList();
// ViewBag.result = result.ToString();
return View(result);
}
Please don't confuse about what i return in action method, its a very long and unnecessary code for that question.
By using this code I successfully get the search result from one table. Now I want same result from all the tables present in the database. that's where I'm stuck. I searched so many article for that but didn't find any solution that's in the last I myself asking this.
Thanks
This may work
Public ActionResult Index(string searchString)
{
var query1 = (from c in db.TableArticle select c);
if (!String.IsNullOrEmpty(searchString))
{
query1 = query1.Where(s =>
s.Article_Title.Contains(searchString)
|| s.Article_Description.Contains(searchString) ||
s.Written_By.Contains(searchString) ||
s.Organisation.Contains(searchString)
||s.Source.Contains(searchString));
}
var query2 = (.......);
var query3 = (.......);
var finalResult = query1.Select(x => x.columnName).Union(query2.Select(x => x.columnName)).Union(query3.Select(x => x.columnName));
// OR
var finalResult = query1.Select(x => x.columnName).Concat(query2.Select(x => x.columnName)).Concat(query3.Select(x => x.columnName));
}
I have just implemented and checked in my local same query, and it's working fine, The given answer is right by @Khairul Alam,
I have just optimized the code and run the same thing in my local its working fine
var searchString = "School";
if (!String.IsNullOrEmpty(searchString))
{
var query1 = _context.Jobs.Where(s =>
s.JobTitle.Contains(searchString)
|| s.LocationDescription.Contains(searchString));
var query2 = _context.Recruiters.Where(s =>
s.RecruiterName.Contains(searchString));
var query3 = _context.Sectors.Where(s =>
s.SectorName.Contains(searchString));
try
{
var finalResult = query1.Select(x => x.JobTitle).Union(query2.Select(x => x.RecruiterName)).Union(query3.Select(x => x.SectorName)).ToList();
}
catch (Exception)
{
throw;
}
contain image which output of final result
If you still getting an issue , please share some code or where exactly you getting the error , so we can solve them
hope this will be helpful to you
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.