2014-02-14 88 views
0

我想將我的聯合lINQ表達式中的兩個表的所有列都發送到我的視圖。 它基本上是客戶記錄以及客戶地址記錄。這是在VB中,但一個C#答案會很好地做。linq實體加入表格不轉換結果到視圖

我不確定我的方法是否正確,並且無法將結果轉換爲視圖期望的結果。

我的結果類定義爲

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 

我的控制器的定義是這樣的: 功能詳解(可選BYVAL ID作爲整數= Nothing)作爲的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'類型的模型項。

+0

當我嘗試消解時,從return語句中得到一個錯誤。 – user3058400

+0

錯誤是LINQ to Entities無法識別方法'System.Collections.Generic.List'1 [MvCAdventureWorkTest.CustomerAddress] ToList [CustomerAddress](System.Collections.Generic.IEnumerable'1 [MvCAdventureWorkTest.CustomerAddress])''方法,並且此方法不能轉換爲商店表達式。 – user3058400

+0

同樣的事情在這裏LINQ to Entities不能識別方法'System.Collections.Generic.List'1 [MvCAdventureWorkTest.CustomerAddress] ToList [CustomerAddress](System.Collections.Generic.IEnumerable'1 [MvCAdventureWorkTest.CustomerAddress])''方法,並且此方法不能轉換爲商店表達式。 – user3058400

回答

0

我已經得出結論,你不能或不能輕易convery類型的對象IQueryable的一個可用的類型要麼的IEnumerable或列表(的),但到底是什麼工作如下:

昏暗的CC作爲新CustomerContext CC .cust =(從c中db.Customers Where(c.CustomerID = id)選擇c).ToList cc.addr =(從一個In db.CustomerAddresses Where(a.CustomerID = id)選擇a).ToList()

返回視圖(cc)