从视图中的LINQ查询中选择“按键值”

时间:2013-12-18 23:35:45

标签: vb.net asp.net-mvc-3 linq entity-framework razor

我试图从LINQ查询中获取每个键值并将它们拉入我的视图中。 LINQ查询如下所示:

Public Property ByVDN As IEnumerable
    Get
        Dim valQ = (From scr In Var.db.CareSideA.ScriptCrossReferences
                   Join s In Var.db.CareSideA.Scripts On s.ScriptID Equals scr.ScriptID
                   Join ms In Var.db.CareSideA.MasterScripts On s.MasterScriptID Equals ms.MasterScriptID
                   Join svce In Var.db.CareSideA.Services On svce.SkillTargetID Equals scr.ForeignKey
                   Join p In Var.db.CareSideA.Peripherals On svce.PeripheralID Equals p.PeripheralID
                   Join sm In Var.db.CareSideA.ServiceMembers On svce.SkillTargetID Equals sm.ServiceSkillTargetID
                   Join sg In Var.db.CareSideA.SkillGroups On sm.SkillGroupSkillTargetID Equals sg.SkillTargetID
                   Where s.Version = ms.CurrentVersion And scr.TargetType = 1 And svce.PeripheralNumber = Value
                   Select New With {Key .Service = svce.PeripheralNumber,
                                    Key .ScriptName = ms.EnterpriseName,
                                    Key .Peripheral = p.EnterpriseName,
                                    Key .SkillMapping = sg.PeripheralNumber,
                                    Key .LatestVersion = s.Version,
                                    Key .Created = s.DateTime,
                                    Key .Author = s.Author}).ToList
        Return valQ
    End Get
    Set(value As IEnumerable)
    End Set
End Property

现在这确实会返回结果,但它们看起来像这样:Screen Shot

理想情况下,我希望能够做到这一点:

<table>
For Each Item In Model.ByVDN
  Dim i = Item
    <tr>
      <td>@Html.DisplayFor(Function(m) i.Service)</td>
      <td>@Html.DisplayFor(Function(m) i.ScriptName)</td>
      <td>@Html.DisplayFor(Function(m) i.Peripheral)</td>
   Next

等...

1 个答案:

答案 0 :(得分:1)

您无法传递匿名对象。 Well, you can, but it is not strongly typed。您必须使用这些属性定义一个类,创建该类的IEnumerable实例并将该Enumerable传递给视图。没有其他办法。

UPD:看到类似的问题:passing linq select query to the method