显示来自linq查询的数据时出现问题

时间:2019-01-16 12:32:08

标签: c# asp.net asp.net-mvc linq

我尝试使用linq显示来自两个表的数据,没有错误,但是我认为我的数据没有到达视图。调试代码时,我看到linq处理了正确的信息。运行我的应用程序后,仅显示@Html.DisplayNameFor不会显示任何数据。

var details = from l in db.LogDetails.ToList()
              join a in db.AuditLog.ToList()
                 on l.AuditLogId equals a.AuditLogId into b
              from d in b.DefaultIfEmpty()
              where d.RecordId.Equals(id)
              select new AuditLogDetail
                      {
                          PropertyName = l.PropertyName,
                          OriginalValue = l.OriginalValue,
                          NewValue = l.NewValue
                      };

return View(details.ToList());

全视图

@model IEnumerable<TrackerEnabledDbContext.Common.Models.AuditLogDetail>

@{
    ViewBag.Title = "Lista zgłoszeń";
}

<h2>Lista zgłoszeń</h2>

<p>
    @Html.ActionLink("Nowe zgłoszenie", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.PropertyName)

        </th>
        <th>
            @Html.DisplayNameFor(model => model.OriginalValue)

        </th>
        <th>
            @Html.DisplayNameFor(model => model.NewValue)
        </th>

    </tr>

    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PropertyName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OriginalValue)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NewValue)
        </td>
    </tr> 
    }

</table>

Screenshot of information that show up after debugging of return View(details.ToList());

我将linq查询更改为:

var r = from s in db.LogDetails
        select s;

,并且表中的每个值都正确显示,但是我需要根据另一个表过滤数据,因此当我尝试以下代码时,会出现错误:

  

无法创建类型为'System.Object'的常量值。在这种情况下,仅支持基本类型或枚举类型。

代码:

var r = from s in db.LogDetails
        join l in db.AuditLog
             on s.AuditLogId equals l.AuditLogId
        where l.RecordId.Equals(id)
        select s;

我已将其更改为==,但随后将无法编译。

1 个答案:

答案 0 :(得分:0)

尝试替换。等于==

    var r = from s in db.LogDetails
    join l in db.AuditLog
         on s.AuditLogId equals l.AuditLogId
    where l.RecordId == id
    select s;