Asp.net mvc 4 linq到未将结果转换为视图的已联接表的实体
我想将联接的lINQ表达式中两个表的所有列都发送到视图中。 它基本上是一个客户记录和客户地址记录。它是用VB编写的,但是用C语言回答就可以了 我不确定我的方法是否正确,无法将结果转化为视图所期望的结果 我的结果类定义为Asp.net mvc 4 linq到未将结果转换为视图的已联接表的实体,asp.net-mvc-4,linq-to-entities,Asp.net Mvc 4,Linq To Entities,我想将联接的lINQ表达式中两个表的所有列都发送到视图中。 它基本上是一个客户记录和客户地址记录。它是用VB编写的,但是用C语言回答就可以了 我不确定我的方法是否正确,无法将结果转化为视图所期望的结果 我的结果类定义为 Partial Public Class CustomerContext Private m_customer As List(Of Customer) Public Property cust As List(Of Customer) Get
Partial Public Class CustomerContext
Private m_customer As List(Of Customer)
Public Property cust As List(Of Customer)
Get
cust = m_customer
End Get
Set(value As List(Of Customer))
m_customer = value
End Set
End Property
Private m_address As List(Of CustomerAddress)
Public Property addr As List(Of CustomerAddress)
Get
addr = m_address
End Get
Set(value As List(Of CustomerAddress))
m_address = value
End Set
End Property
End Class
我的控制器定义如下:
函数DetailsOptional ByVal id为整数=ActionResult为零
Dim cc = (From c In db.Customers Join a In db.CustomerAddresses On c.CustomerID Equals a.AddressID
Where (c.CustomerID = id)
Select New CustomerContext With {.addr = db.CustomerAddresses.ToList, .cust = db.Customers.ToList})
If IsNothing(cc) Then
Return HttpNotFound()
End If
Return View(cc)
End Function
我的观点是
@Modeltype MvCAdventureWorkTest.CustomerContext
<table>
<tr class="displayline">
<td>
<table>
<tr>
<td>
@Html.DisplayNameFor(Function(model) model.cust.Item("Name"))
</td>
<td class="displaydatahalf">
@Html.DisplayFor(Function(model) model.cust.item("Name"))
</td>
</tr>
<tr>
<td >
@Html.DisplayNameFor(Function(model) model.addr.item("address"))
</td>
<td>
@Html.DisplayFor(Function(model) model.addr.item("address"))
</td>
</tr>
我收到的错误消息是
传递到字典中的模型项的类型为“System.Data.Objects.ObjectQuery`1[MvCAdventureWorkTest.CustomerContext]”,但此字典需要类型为“MvCAdventureWorkTest.CustomerContext”的模型项 我的结论是,您不能或不能轻松地将iqueryable类型的对象转换为ienumerable或listof类型的可用对象,但有效的方法如下: Dim cc作为新的CustomerContext cc.cust=来自数据库中的c.Customers,其中c.CustomerID=id选择c.ToList cc.addr=来自db.CustomerAddresses中的a.CustomerID=id选择a.ToList
return viewcc当我尝试该sugestion时,我从return语句中得到一个错误。错误是LINQ to Entities无法识别方法“System.Collections.Generic.List1[MvCAdventureWorkTest.CustomerAddress]ToList[CustomerAddress]System.Collections.Generic.IEnumerable1[MvCAdventureWorkTest.CustomerAddress]”方法,并且此方法无法转换为存储表达式。与此相同,LINQ to Entities无法识别方法“System.Collections.Generic.List1[MvCAdventureWorkTest.CustomerAddress]ToList[CustomerAddress]System.Collections.Generic.IEnumerable1[MvCAdventureWorkTest.CustomerAddress]”方法,这个方法不能被转换成存储表达式。我明白了,你不能在你到CustomerContext的投影中列出它。将这些属性改为IEnumerable,删除ToList并尝试一下。这就是我真正迷路的地方。您使用的是什么属性,CustomerContext类还是该类的组件?