正如我的標題所說,我在嘗試創建一些實體SQL請求時遇到了麻煩。錯誤表示表達式必須是CollectionType。
這裏的整個代碼都必須用作搜索引擎。我需要構建字符串,然後將它們轉換爲查詢。我已經用linq-to-sql完成了所有的事情。但將字符串轉換爲linq查詢似乎是不可能的。然而,我在stackoverflow上找到了一種解決方案,但我從來沒有能夠使用它:String.ToLinqQuery()。這只是視覺工作室不知道的,我找不到任何關於它的文檔。
雖然,這裏的錯誤,我得到:
The specified expression must be of CollectionType. Near parenthesized expression, line 1, column 20.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.EntitySqlException: The specified expression must be of CollectionType. Near parenthesized expression, line 1, column 20.
Source Error:
Line 145: Meetings
Line 146:
Line 147: @For Each meeting In Model.Meetings
Line 148: @@meeting.date
Line 149: @Html.ActionLink("Edit", "Edit", "Meeting", New With {.id = meeting.idmeeting}, "Null")
,這裏是我的所有代碼:控制器:
Imports System.Data.Objects
Imports System.Linq.Expressions
Namespace MvcApplication4
Public Class SearchController
Inherits System.Web.Mvc.Controller
<HttpPost()>
Function Index(search As String, choix As Integer) As ActionResult
Dim test As Integer = Request("choix")
Dim chaine As String = Request("searchString")
Dim message As String = "message"
Dim requete As String = "Select value p FROM('db.meeting') as p where p.compteRendu LIKE '%chaine%'"
Dim meetings = New ObjectQuery(Of meeting)(requete, db)
Dim model = New SearchModel With {
.Meetings = meetings,
}
Return View(model)
End Function
End Class
End Namespace
型號:
Public Class SearchModel
Public Property Meetings As IEnumerable(Of meeting)
End Class
查看:
@Modeltype MvcApplication4.SearchModel
@<fieldset>
<legend>Meetings</legend>
@For Each meeting In Model.Meetings
@<ul> @meeting.date
@Html.ActionLink("Edit", "Edit", "Meeting", New With {.id = meeting.idmeeting}, "Null") </ul>
@<li> @Html.Raw(meeting.compteRendu.Replace(System.Environment.NewLine, "<br />"))</li>
Next meeting
</fieldset>
我在做什麼錯?
這裏是我的.edmx模型
<EntityType Name="meeting">
<Key>
<PropertyRef Name="idmeeting" />
</Key>
<Property Name="idmeeting" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="FK_meet_client" Type="int" />
<Property Name="FK_meet_contact" Type="int" />
<Property Name="FK_meet_opport" Type="int" />
<Property Name="FK_meet_user" Type="int" />
<Property Name="compteRendu" Type="longtext" />
<Property Name="date" Type="datetime" />
<Property Name="adresse" Type="text" />
</EntityType>
它不會改變任何東西。我認爲這是因爲我沒有收集,但我不知道如何... – 2012-03-15 10:08:51
所以它最可能意味着'db.meeting'不是您模型中設置的實體的有效名稱。 – 2012-03-15 10:11:06
當我使用Linq時,我的查詢運行良好。我已經改變了,因爲我需要更多的動力。我認爲這個問題可能在「ObjectQuery」的參數中。 – 2012-03-15 11:55:31