2012-03-15 4 views
0

필자의 제목에서 일부 엔티티 SQL 요청을 시도하는 데 문제가 있습니다.오류는 표현식이 CollectionType이어야한다고 말합니다.

전체 코드는 검색 엔진으로 사용해야합니다. 문자열을 만든 다음 쿼리로 변환해야합니다. 나는 linq-to-SQL을 사용하여 모든 것을 이미 수행했다. 그러나 문자열을 linq 쿼리로 변환하는 것은 불가능합니다. 그러나, 나는 stackoverflow에서 일종의 솔루션을 발견했지만 결코 사용할 수 없었습니다 : String.ToLinqQuery(). 그것은 Visual Studio에서 알려지지 않았기 때문에 이에 대한 문서를 찾을 수 없습니다. 여기에 내 모든 코드

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> 

있지만, 여기에 오류가 내가 얻을입니다

내가 뭘 잘못하고 있니? 여기

는 ESQL 쿼리 부분에서 내 .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에 대한 술어 작성자를 사용하여 내 검색 작업을하는 또 다른 방법을 사용했습니다. 여기 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 

의 predicatebuilder의 코드가 그리고 여기에 내가 그것을 사용하는 방법이다

predicatebuilder의 코드가 촬영되었습니다 (그리고 VB로 번역) http://www.albahari.com/nutshell/predicatebuilder.aspx

에서 :

 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

의 일부가 잘못된 것 같습니다. 사용해보기 :

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

아무런 변화가 없습니다. 나는 컬렉션을 얻지 못했기 때문에 그런 것 같지만, 어떻게해야할지 모르겠다. ... –

+0

아마도 db.meeting은 모델에 설정된 엔티티의 유효한 이름이 아닐 수도있다. –

+0

Linq를 사용했을 때 쿼리가 잘 작동했습니다. 좀 더 역동적 인 것이 필요했기 때문에 나는 바꿨다. 나는이 문제가 "ObjectQuery"의 매개 변수 안에 있다고 생각한다. –