Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store pieces of Linq expression in variables and use those variables in a query?

I have an expression like this:

List<BAL.Receipt> ac = BAL.ApplicationInfo.db.Receipts.Where
                (x => x.InvoiceNo.StartsWith(txtSearch.Text)
                 | x.Alias.StartsWith(txtSearch.Text)

What I want to do is split this expression into parts and store them in variables

Like

var a = x => x.InvoiceNo.StartsWith(txtSearch.Text);
var b = x => x.Alias.StartsWIth (txtSearch.Text) ; 

When query

List<BAL.Receipt> ac = BAL.ApplicationInfo.db.Receipts.Where( a & b) ; 

Is it possible to achieve this?

If possible, please show me an example.

like image 314
Kas Avatar asked Nov 22 '25 11:11

Kas


1 Answers

You should be able to do it like this:

Expression<Func<BAL.Receipt, bool>> a =
    x => x.InvoiceNo.StartsWith(txtSearch.Text);

Expression<Func<BAL.Receipt, bool>> b =
    x => x.Alias.StartsWIth(txtSearch.Text);

List<BAL.Receipt> ac =
    BAL.ApplicationInfo.db.Receipts
        .Where(a)
        .Where(b);
like image 198
Enigmativity Avatar answered Nov 24 '25 01:11

Enigmativity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!