views:

31

answers:

1

Hi, I have four tables in my database and i want to join that's and return record's and show that into searchController!

My query is this:

public IQueryable PerformSearch(string query)
{
    if (!string.IsNullOrEmpty(query))
    {
        var results = from tbl1 in context.Table1
                      join tbl2 in context.Table2 on tbl1.Id equals tbl2.Id
                      join tbl3 in context.Table3 on tbl2.Id equals tbl3.Id
                      join tbl4 in context.Table4 on tbl3.Id equals tbl4.Id
                      where tbl1.col2.Contains(query)
                      orderby tbl1.Count descending
                      select new
                      {
                          col1 = tbl1.Col1,
                          col1 = tbl1.Col1,
                          col1 = tbl1.Col1,
                                  .
                                  .
                                  .
                      };
        return results.AsQueryable();
    }
    else return null;
}

And this method called in SearchController as below:

public class SearchController : System.Web.Mvc.Controller
{
    public System.Web.Mvc.ActionResult Search(System.String query)
    {
        var search = new Search();
        ViewData["result"] = search.PerformSearch(query);
        return View("Search");
    }
}

I don't know how i can rotate around each record (plus vs intellisense feature) returned by PeformSeach method and show that in view! Also is this a good way?

thanks in advance

+1  A: 

I would recommend using a strongly typed View for this.

First of all return a model object from PerformSearch instead of IQueryable. Set this model object to your View.

Once your View knows what kind of data it needs to display then you can use intellisense.

public class Record
{
    public int Col1 { get; set; }
    public string Col2 { get; set; }
    ...
}

public IList<Record> PerformSearch(string query)
{
    var records = new List<Record>();

    if (!string.IsNullOrEmpty(query))
    {
        return (from tbl1 in context.Table1
                join tbl2 in context.Table2 on tbl1.Id equals tbl2.Id
                join tbl3 in context.Table3 on tbl2.Id equals tbl3.Id
                join tbl4 in context.Table4 on tbl3.Id equals tbl4.Id
                where tbl1.col2.Contains(query)
                orderby tbl1.Count descending
                select new Record
                {
                    col1 = tbl1.Col1,
                    col1 = tbl1.Col1,
                    col1 = tbl1.Col1
                }).ToList();
    }

    return records;
}

//ActionMethod
public System.Web.Mvc.ActionResult Search(System.String query)
{
    var search = new Search();
    var model = search.PerformSearch(query);
    return View(model);
}

//View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Collections.IList<Record>>" %>

<% foreach (var record in Model) { %>
    <%= Html.Encode(record.Col1) %> --this will now give you intellisense
<% } %>
David Liddle