2012-03-15 55 views
0

正如我的標題所說,我在嘗試創建一些實體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> 

回答

0

我沒有真正找到解決方案。我用另一種方式來處理我的搜索,使用Linq的predicatebuilder。

的predicatebuilder的代碼的情況下(和翻譯爲VB)從http://www.albahari.com/nutshell/predicatebuilder.aspx

這裏是predicatebuilder在VB

Imports System.Linq 
Imports System.Linq.Expressions 
Imports System.Collections.Generic 
Imports System.Runtime.CompilerServices 

Module PredicateBuilder 
Sub New() 
End Sub 
Public Function [True](Of T)() As Expression(Of Func(Of T, Boolean)) 
    Return Function(f) True 
End Function 
Public Function [False](Of T)() As Expression(Of Func(Of T, Boolean)) 
    Return Function(f) False 
End Function 

<System.Runtime.CompilerServices.Extension()> 
Public Function [Or](Of T)(expr1 As Expression(Of Func(Of T, Boolean)), expr2 As Expression(Of Func(Of T, Boolean))) As Expression(Of Func(Of T, Boolean)) 
    Dim invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast(Of Expression)()) 
    Return Expression.Lambda(Of Func(Of T, Boolean))(Expression.[OrElse](expr1.Body, invokedExpr), expr1.Parameters) 
End Function 

<System.Runtime.CompilerServices.Extension()> 
Public Function [And](Of T)(expr1 As Expression(Of Func(Of T, Boolean)), expr2 As Expression(Of Func(Of T, Boolean))) As Expression(Of Func(Of T, Boolean)) 
    Dim invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast(Of Expression)()) 
    Return Expression.Lambda(Of Func(Of T, Boolean))(Expression.[AndAlso](expr1.Body, invokedExpr), expr1.Parameters) 
End Function 
End Module 

的代碼,這是我如何使用它:

 Dim predicate1 = PredicateBuilder.False(Of meeting)() 
     For Each mot In tabMot 
      predicate1 = predicate1.Or(Function(m) m.compteRendu.Contains(mot)) 
     Next 
     Dim meetings = db.meeting.AsExpandable().Where(predicate1) 
1

的在你的ESQL查詢部分來自部分似乎是錯誤的。嘗試使用:

Select value p FROM db.meeting as p where p.compteRendu LIKE '%chaine%' 
+0

它不會改變任何東西。我認爲這是因爲我沒有收集,但我不知道如何... – 2012-03-15 10:08:51

+0

所以它最可能意味着'db.meeting'不是您模型中設置的實體的有效名稱。 – 2012-03-15 10:11:06

+0

當我使用Linq時,我的查詢運行良好。我已經改變了,因爲我需要更多的動力。我認爲這個問題可能在「ObjectQuery」的參數中。 – 2012-03-15 11:55:31

相關問題