Cannot implicity convert type System.Linq.Expression<System.Func<Object, bool>> to bool

actually I have an Expression like this that work very well in the case of Linq to Entity

public static Expression<Func<Tender, bool>> GetPublic()
{
    var now = DateTime.Now.GetEasternStandardDateTime();

    return tender => tender.EndDate > DateTime.Now &&
                     tender.IsClosed == false && 
                     tender.IsCancel == false && 
                     tender.PrivacyLevelId == 1;
}

I can use the expression like this : _repositoryObject.Where(GetPublic()) and I will get results.

But I cannot use the same expression when I have to do Linq to SQL pattern like this

var results = from t in Tenders
              where Tender.GetPublic()
              select t;

I have this error

Cannot implicity convert type System.Linq.Expression> to bool for Linq to SQL

and I don't know why...

Karine

Jon Skeet
people
quotationmark

Firstly, I suspect you actually mean you can call it as:

_repositoryObject.Where(GetPublic())

It's important that you call the method to get the expression tree.

Your query expression is being converted into:

var results = Tenders.Where(t => Tender.GetPublic());

... which isn't what you're looking for. Basically, query expressions are translated into method calls using lambda expressions. You don't want that in this case, so you shouldn't use a query expression.

If you want to do other things with a query expression, you can always mix and match, e.g.

var results = from t in Tenders.Where(Tender.GetPublic())
              where t.SomeOtherProperty
              select t.SomethingElse;

people

See more on this question at Stackoverflow