【问题标题】:Errors say that an expression must be of CollectionType.错误说表达式必须是 CollectionType。
【发布时间】:2012-03-31 17:58:32
【问题描述】:

正如我的标题所说,我在尝试发出一些实体 SQL 请求时遇到了麻烦。

这里的整个代码必须用作搜索引擎。我需要构建字符串,然后将它们转换为查询。我已经用 linq-to-sql 完成了所有工作。但似乎不可能将字符串转换为 linq 查询。但是,我在 stackoverflow 上找到了一种解决方案,但我一直无法使用它:String.ToLinqQuery()。 Visual Studio 不知道它,我找不到任何关于它的文档。

虽然,这是我得到的错误:

指定的表达式必须是 CollectionType。近括号表达式,第 1 行,第 20 列。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.EntitySqlException:指定的表达式必须是 CollectionType。近括号表达式,第 1 行,第 20 列。

来源错误:

第 145 行:会议
第 146 行:
第 147 行:@For Each meeting In Model.Meetings
第 148 行:@

    @meeting.date
    第 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>

【问题讨论】:

    标签: .net vb.net linq asp.net-mvc-3 entity-framework


    【解决方案1】:

    您的 ESQL 查询中的 FROM 部分似乎是错误的。尝试使用:

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

    【讨论】:

    • 它不会改变任何东西。我想这是因为我没有收藏,但我不知道如何...
    • 所以这很可能意味着db.meeting 不是模型中实体集的有效名称。
    • 当我使用 Linq 时,我的查询运行良好。我改变了,因为我需要一些更有活力的东西。我认为问题可能出在“ObjectQuery”的参数中。
    • 但是 LINQ 中设置的实体名称与映射的 CSDL 部分中的名称不同。 ESQL 使用来自 CSDL 的名称。您可以在 EDMX 文件中找到它。
    • 我有一个解决方案,但我还需要 3 小时才能回答。我希望我不会忘记。
    【解决方案2】:

    我并没有真正找到解决方案。我使用了另一种方式来处理我的搜索,使用 Linq 的谓词构建器。

    谓词构建器的代码已从http://www.albahari.com/nutshell/predicatebuilder.aspx 获取(并翻译为VB)

    这是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)
    

    【讨论】:

      猜你喜欢
      • 2014-12-13
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多