Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

E.F. lambda expression for a List<string> with StartsWith

I'm developing an Entity Framework 6.1.2 library with .NET Framework 4.5.1 and C#.

I have this predicate:

Expression<Func<EXTERNAL_CODES, bool>> predicate = null;
List<string> codes = GetStartEntCodes(startingCode, quantity);

predicate = (e =>
        e.USED == 0
    && codes.Contains(e.CODE)
    && e.CHINA_CODES_HEADER_ID == batch.Id
    && e.CODE_LEVEL == codeLevel
    && (e.BATCH_ID == batch.Id || e.BATCH_ID == null));

But this part && chinaCodes.Contains(e.CODE) doesn't work.

e.CODE is nvarchar(20) and each string on codes list contains only the first 15 characters.

I want to do this:

`WHERE CODE LIKE `codes.value%'; -- Note: `codes.value%' represents each value in codes list.`

How can I do this?

like image 945
VansFannel Avatar asked Sep 17 '26 14:09

VansFannel


1 Answers

You can try something like that:

        predicate = (e =>
                e.USED == 0
            && codes.Any(x => x.StartsWith(e.CODE))
            && e.CHINA_CODES_HEADER_ID == batch.Id
            && e.CODE_LEVEL == codeLevel
            && (e.BATCH_ID == batch.Id || e.BATCH_ID == null));
like image 114
Bruniasty Avatar answered Sep 19 '26 04:09

Bruniasty



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!