如何围绕 linq 中的每条记录旋转并在视图中显示该记录

发布于 2024-09-02 16:01:18 字数 1377 浏览 6 评论 0原文

我的数据库中有四个表,我想加入它并返回记录并将其显示到 searchController 中!

我的查询是这样的:

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;
}

这个方法在 SearchController 中调用如下:

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");
    }
}

我不知道如何围绕 PeformSeach 方法返回的每条记录(加上与智能感知功能)进行旋转并在视图中显示!这也是个好办法吗?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-09-09 16:01:18

我建议为此使用强类型视图

首先从 PerformSearch 返回一个模型对象而不是 IQueryable。将此模型对象设置为您的视图。

一旦您的视图知道需要显示什么类型的数据,您就可以使用智能感知。

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
<% } %>

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
<% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文