VB.NET实体框架问题与WHERE子句和布尔值

时间:2011-10-17 20:37:02

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

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

错误13重载解析失败,因为无法使用这些参数调用可访问的“Where”:     扩展方法'Public Function Where(谓词As System.Func(Of Review,Integer,Boolean))As System.Collections.Generic.IEnumerable(Of Review)'在'System.Linq.Enumerable'中定义':类型'Boolean'的值无法转换为'System.Func(Of PowellCasting.Models.Review,Integer,Boolean)'。     扩展方法'Public Function Where(谓词As System.Func(Of Review,Boolean))As System.Collections.Generic.IEnumerable(Of Review)'在'System.Linq.Enumerable'中定义':'Boolean'类型的值不能是转换为'System.Func(Of PowellCasting.Models.Review,Boolean)'。     扩展方法'Public Function Where(谓词As System.Linq.Expressions.Expression(Of System.Func(Of Review,Integer,Boolean)))As System.Linq.IQueryable(Of Review)'在System.Linq.Queryable中定义':类型'Boolean'的值无法转换为'System.Linq.Expressions.Expression(Of System.Func(Of PowellCasting.Models.Review,Integer,Boolean))'。     扩展方法'Public Function Where(谓词As System.Linq.Expressions.Expression(Of System.Func(Of Review,Boolean)))As System.Linq.IQueryable(Of Review)'在'System.Linq.Queryable'中定义':类型'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

结束课程 结束命名空间

2 个答案:

答案 0 :(得分:4)

您需要指定lambda表达式:

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

或者只是:

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

答案 1 :(得分:1)

试试这个:

Return PowellCastingDB.Reviews.Where(Function(review) review.Active = True)
相关问题