1

I am trying to use the answer from LINQ to SQL Where Clause Optional Criteria

I like my linq to use query based syntax. Not sure how to use the whereif.

my query will look something like this

var result = (from tran in _ctx.Transactions where tran.Id == transactionId select tran);

and sometimes projecting it

var result = (from tran in _ctx.Transactions where tran.Id == transactionId
     select new Abstract
           {
               tran.Date,
               tran.Key
           });

I can do an optional filter using method syntax

var result = _ctx.Transactions
             .where(t=>t.Id == transactionId)
             .whereIf(tran.Dept!= "AllDept", x => x.Dept== deptName);

Not sure how to use the WhereIf in a query based linq query.

2
  • See stackoverflow.com/questions/279701/… for a discussion about which syntax is better particularly when you are extending it. Commented Apr 3, 2018 at 5:58
  • That syntax is pretty crippled, so best you can do is: from tran in _ctx.Transactions.WhereIf(..., tran => tran.Id == transactionId) select tran (so a mix of two syntaxes). Commented Apr 3, 2018 at 6:27

1 Answer 1

1

Read : Dynamic query with Linq

I suggest make use of PredicateBulder and add predicate to you query , like as below , this will build query dynamically with variable predicate

var predicate = PredicateBuilder.True<Transaction>();  
predicate = predicate.And(t=>t.Id == transactionId);  
if (!string.IsNullOrEmpty(department))  
{  
    predicate = predicate.And(tran => tran.Dept!= "AllDept");  
}
var result = _ctx.Transactions.where(predicate);

Dynamically Composing Expression Predicates

Sign up to request clarification or add additional context in comments.

4 Comments

What do I do incase of projections? Say I am using joins in the query for example var tran = (from t in _ctx.transactions join type in _ctx.TranType on t.key equals type.key select new Abstract {t.date, type.name}); now I want to filter based on an optional filter on tranType.?
@Navyseal - if in given query you want to add filter based on tranType , is trantype is coming from query ??
tranType is another table which I wish to join with transaction. Actually I have multiple joins. But for brevity, lets say I have two table transaction and transactionType and I am basing my filter on an Id which is in Transaction and I want to add an optional filter on TransactionType.Name only if say the function argument doesnt say "AllTypes" and if the argument says "AllTypes", I leave out the filter. All this needs to be projected into another "AbstractTransaction" class. How do I do this using predicate builder?
@Navyseal - not geting actual view of your code but need to do like this if (transtype == "AllTypes") { predicate = predicate.And(tran => condition you want); }

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.