1

I'm pretty new to MVC and the Entity Framework, but I'm sure this should be straight forward. I've got a class with a boolean "Active" flag. I then have a function that returns the results by date descending. All I want to do is ensure that only active records are returned. Should be simple, but it fails with the following error:

Error 13 Overload resolution failed because no accessible 'Where' can be called with these arguments: Extension method 'Public Function Where(predicate As System.Func(Of Review, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of Review)' defined in 'System.Linq.Enumerable': Value of type 'Boolean' cannot be converted to 'System.Func(Of PowellCasting.Models.Review, Integer, Boolean)'. Extension method 'Public Function Where(predicate As System.Func(Of Review, Boolean)) As System.Collections.Generic.IEnumerable(Of Review)' defined in 'System.Linq.Enumerable': Value of type 'Boolean' cannot be converted to 'System.Func(Of PowellCasting.Models.Review, Boolean)'. Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Review, Integer, Boolean))) As System.Linq.IQueryable(Of Review)' defined in 'System.Linq.Queryable': Value of type 'Boolean' cannot be converted to 'System.Linq.Expressions.Expression(Of System.Func(Of PowellCasting.Models.Review, Integer, Boolean))'. Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Review, Boolean))) As System.Linq.IQueryable(Of Review)' defined in 'System.Linq.Queryable': Value of type 'Boolean' cannot be converted to '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

It looks as thought it doesn't like comparing Booleans but they have the same data type. I'm sure this should be simple, but would appreciate some help. Please see my code below.

Public Class Review

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

End Class End Namespace

1
  • I write in C# so I'm not providing this as an answer, but wouldn't you need something like .Where(Function(review) review.Active = True)? Commented Oct 17, 2011 at 20:43

2 Answers 2

4

You need to specify a lambda expression:

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

Or just:

Return PowellCastingDB.Reviews.Where(Function(x) x.Active).
    (rest of query)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick response Jon - I thought I tried this and it didn't return any results (though no error), but this does indeed work. Thanks for that :) I'm fairly new to this, so I'm not really sure what a lambda expression is - why would I need a function for a where clause? I'm sure I'll get my head round it.
Where works by using a function that evaulates if the condition is met. If so, it "yields" the result on to other functions. By making Where take a function that returns a Boolean, we have a generic implementation to accomplish this task regardless of what the condition is that your query needs to assert.
1

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.