Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 862
Karine Avatar asked Jul 03 '26 23:07

Karine


1 Answers

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;
like image 70
Jon Skeet Avatar answered Jul 05 '26 13:07

Jon Skeet