简体   繁体   English

通过链接查询结束n + 1问题以连接多个表

[英]End the n+1 issue through Link Query to join multiple tables

I have a Linq query that I currently pull out a table and then in the view I filter 5 joins down to a specific value. 我有一个Linq查询,当前我已拉出一个表,然后在视图中将过滤器5联接为一个特定值。 This as many of you know would greatly increase the amount of time for my view to load as for each record has to run its own individual query for each of the joins. 你们中许多人都知道,这将极大地增加我的视图加载时间,因为每个记录必须为每个联接运行自己的查询。 I want to switch the joins in to the Linq query, so it is all in one query but I am having trouble. 我想将联接切换到Linq查询,所以它是所有查询中的一个,但遇到了麻烦。

Here is my controller code: 这是我的控制器代码:

private IEnumerable<Ticket> FindTechTickets(Guid ticketStatusId)
{
       Guid g = Guid.Parse(Session["LoggedUserID"] as string);

       return db.Tickets
              .Include(t => t.TicketNotes)
              .Where(t => t.TechnicianId == g).Where(t => t.TicketStatusId == ticketStatusId)
              .OrderByDescending(t => t.TicketNumber)
              .ToList();

}

And here is my view code: 这是我的查看代码:

@foreach (var item in Model.OrderByDescending(m => m.TicketNumber))
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.TicketNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OpenDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OpenUser.FullName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Category.CategoryName)
        </td>
        <td>
            <div>
                @Html.DisplayFor(modelItem => item.TicketNotes.OrderBy(t => t.TicketNoteDate).Last().TicketNoteDate)
            </div>
        </td>
        <td>
            <div>
                @Html.DisplayFor(modelItem => item.TicketNotes.OrderBy(t => t.TicketNoteDate).Last().UserNote.FullName)
            </div>
        </td>
        <td>
            <div style="overflow:auto; width:300px;">
                @Html.DisplayFor(modelItem => item.TicketNotes.OrderBy(t => t.TicketNoteDate).First().Note)
            </div>
        </td>
        <td>
            @Html.ActionLink("Open/Edit", "EditTechTicket", new { id = item.TicketId, returnUrl = "TechOpenTickets", ViewBag.page})
        </td>
    </tr>
}

and here is my attempt at changing the Linq query: 这是我尝试更改Linq查询的尝试:

var ts = 
          from t in db.Tickets
          Join tn in db.TicketNotes on t.TicketId equals tn.TicketId
          Where(t => t.TechnicianId == g).Where(t => t.TicketStatusId == ticketStatusId)
          OrderByDescending(t => t.TicketNumber)
          select new
          {

          }

The problem is am getting build errors on almost every line. 问题是几乎每一行都出现构建错误。 Starting with the Join line. 从加入行开始。

This is the link that I am getting the information from: 这是我从中获取信息的链接:

Pulling out different columns from different tables using LINQ 使用LINQ从不同的表中拉出不同的列

  1. It seems you're missing some parentheses. 似乎您缺少一些括号。

  2. Join is not the method you're looking for. Join不是您要找的方法。 It does something entirely different. 它所做的事情完全不同。 You want Include . 您要Include

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM