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.
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
What I have tried:
public PartialViewResult Searchpartial(string searchString)
{
var article = (from c in db.Tbl_Article select new
SearchModel() { Tbl_Article = c });
article = article.Where(s =>
s.Tbl_Article.Article_Title.Contains(searchString));
var blog = (from c in db.Tbl_Blog select new SearchModel() {
Tbl_Blog = c });
blog = blog.Where(s =>
s.Tbl_Blog.Blog_Title.Contains(searchString));
var history = (from c in db.Tbl_History select new
SearchModel() { Tbl_History = c });
history = history.Where(s =>
s.Tbl_History.Title.Contains(searchString));
var result = article.Select(x =>
x.Tbl_Article.Article_Title).Union(blog.Select(x =>
x.Tbl_Blog.Blog_Title)).Union(history.Select(x =>
x.Tbl_History.Title)).ToList();
if (result != null)
{
return PartialView("Searchpartial", result);
}
else
{
string res = "No Record Found";
return PartialView("Searchpartial", res);
}
}
This is my partialView:
@model IEnumerable<project.Models.SearchModel>
@using Project.Models
<div class="col-md-8">
@{
if (Model.Count() == 0)
{<br />
<br />
<br />
<div class="col-md-12">
<p>
Sorry! No result found
</p>
</div>
}
else
{<br />
<br />
<br />
foreach (var item in Model)
{
<div class="col-md-12">
<p>
<a href="@Url.Action("Article","ArticleView", new { id =
HttpUtility.UrlEncode(new
StandardModule().Encrypt(item.Tbl_Article.Id.ToString()))
})">@item.Tbl_Article.Article_Title</a>
<a href="@Url.Action("Blog","BlogView", new { id =
HttpUtility.UrlEncode(new
StandardModule().Encrypt(item.Tbl_Blog.Id.ToString()))
})">@item.Tbl_Blog.Blog_Title</a>
<a href="@Url.Action("History","HistoryView", new { id =
HttpUtility.UrlEncode(new
StandardModule().Encrypt(item.Tbl_History.Id.ToString()))
})">@item.Tbl_History.Title</a>
</p>
</div>
}
}
}
</div>
Model Class :
public class SearchModel
{
public Tbl_Article Tbl_Article { get; set; }
public Tbl_Blog Tbl_Blog { get; set; }
public Tbl_History Tbl_History { get; set; }
}