【问题标题】:VB.NET Entity Framework issue with WHERE clause and Boolean values带有 WHERE 子句和布尔值的 VB.NET 实体框架问题
【发布时间】:2011-10-17 20:37:01
【问题描述】:

我对 MVC 和实体框架还很陌生,但我相信这应该是直截了当的。我有一个带有布尔“活动”标志的类。然后我有一个按日期降序返回结果的函数。我要做的就是确保只返回活动记录。应该很简单,但它失败并出现以下错误:

错误 13 重载解析失败,因为无法使用以下参数调用可访问的“Where”: 'System.Linq.Enumerable'中定义的扩展方法'Public Function Where(predicate As System.Func(Of Review, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of Review)':'Boolean'类型的值无法转换为“System.Func(Of PowellCasting.Models.Review, Integer, Boolean)”。 'System.Linq.Enumerable' 中定义的扩展方法'Public Function Where(predicate As System.Func(Of Review, Boolean)) As System.Collections.Generic.IEnumerable(Of Review)':'Boolean' 类型的值不能转换为“System.Func(Of PowellCasting.Models.Review, Boolean)”。 'System.Linq.Queryable 中定义的扩展方法'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Review, Integer, Boolean))) As System.Linq.IQueryable(Of Review)' ':'Boolean' 类型的值不能转换为 'System.Linq.Expressions.Expression(Of System.Func(Of PowellCasting.Models.Review, Integer, Boolean))'。 'System.Linq.Queryable'中定义的扩展方法'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Review, Boolean))) As System.Linq.IQueryable(Of Review)': 'Boolean' 类型的值无法转换为 'System.Linq.Expressions.Expression(Of System.Func(Of PowellCasting.Models.Review, Boolean))'。 C:\Web Projects\Powell Casting\PowellCasting\PowellCasting\Models\Review.vb 42 14 PowellCasting

看起来它不喜欢比较布尔值,但它们具有相同的数据类型。我相信这应该很简单,但希望能得到一些帮助。请在下面查看我的代码。

公开课回顾

Private PowellCastingDB As PowellCastingEntites = New PowellCastingEntites

<ScaffoldColumn(False)>
Public Property ReviewID As Integer
<Required(ErrorMessage:="An review title is required")>
<StringLength(256)>
<DisplayName("Title")>
Public Property Title As String
<DisplayName("Heading")>
Public Property Heading As String
<DisplayName("ReviewText")>
<StringLength(4096)>
Public Property ReviewText As String
<DisplayName("Author")>
<StringLength(256)>
Public Property Author As String
<DisplayName("Publication")>
<StringLength(150)>
Public Property Publication As String
<DisplayName("PublicationDate")>
Public Property PublicationDate As Date
<DisplayName("PublicationLink")>
<StringLength(1000)>
Public Property PublicationLink As String
<DisplayName("Image")>
<StringLength(512)>
Public Property Image As String
<DisplayName("Active")>
Public Property Active As Boolean

Public Property Reviews As List(Of Review)

Public Function GetLatestReviews(ByVal count As Integer) As List(Of Review)
  Return PowellCastingDB.Reviews.Where(Active = True).
  OrderByDescending(Function(a) a.PublicationDate).
   Take(count).
    ToList()
End Function

结束类 结束命名空间

【问题讨论】:

  • 我是用 C# 编写的,所以我不提供这个作为答案,但你不需要像 .Where(Function(review) review.Active = True) 这样的东西吗?

标签: asp.net sql vb.net linq entity-framework


【解决方案1】:

你需要指定一个 lambda 表达式:

Return PowellCastingDB.Reviews.Where(Function(x) x.Active = True).
    (rest of query)

或者只是:

Return PowellCastingDB.Reviews.Where(Function(x) x.Active).
    (rest of query)

【讨论】:

  • 感谢乔恩的快速回复——我以为我试过了,但它没有返回任何结果(尽管没有错误),但这确实有效。谢谢你:)我对此很陌生,所以我不太确定什么是 lambda 表达式 - 为什么我需要一个 where 子句的函数?我相信我会明白的。
  • 使用评估条件是否满足的函数来工作。如果是这样,它将结果“产生”给其他功能。通过使 Where 采用返回布尔值的函数,我们有一个通用实现来完成此任务,而不管您的查询需要断言的条件是什么。
【解决方案2】:

试试这个:

Return PowellCastingDB.Reviews.Where(Function(review) review.Active = True)

【讨论】:

    猜你喜欢
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多