Home > Back-end >  How to get a filtered list from an existing list
How to get a filtered list from an existing list

Time:09-18

This project is a .NET Core 6 Blazor PWA hosted and using the shared-"Heading"-model, server-repository, server-controller and client-service tasks.

On the client-side, this filtering does NOT work and am very confused why it does not work.

The Heading model class contains fields for

int UID_HEADING
int UID_CUSTOMER
string TXT_NAME
boolean BOOL_IS_ACTIVE 
int INT_SORT_ORDER 

The IEnumerable Heading _AllHeadings list contains 5 records.

In the UI, there is a dropdown showing the CUSTOMERS (1 to 23).

I need to filter the _AllHeadings based on the customer's pUID_CUSTOMER and I tried this:

_FilteredHeadings = _AllHeadings
    .Where(c => (c.UID_CUSTOMER == pUID_CUSTOMER) 
             && (c.BOOL_IS_ACTIVE == bIsActive))
    .OrderBy(e => e.INT_SORT_ORDER)
    .ToList();

However, the _FilteredHeadings only contains ONE record, even though there should be a subset of the complete list of _AllHeadings.

I am very confused as to why this lambda code does not do what I think it should do.

I am not much experienced in Linq and lambda code.

BTW -- if I change the development-code to

_FilteredHeadings = _AllHeadings;

Then all of the records appear -- for all customers.

Your answers, comments and questions are welcome.

Thanks.

CodePudding user response:

The code that you have provided is on the face of it correct. That is to say, it does what you expect it to do. Therefore the problem presumably lies with the data itself.

This is why providing an MRE is often the solution itself. If you cannot replicate the problem with some simple test data, that is a strong indication that the problem lies not with your code, but with the data itself.

Here it always helps if you have the ability to query the underlying database with an equivalent SQL statement. You did not say, what database you were using. SQL though standard comes in many flavours. Always citing the RDBMS you are using, helps us to construct a test SQL to check your raw data, assuming such help is required.

In this case though, it seems that all you needed was a "rubber duck". Someone to point out the steps to take to solve the problem yourself. Solving it yourself is always the best way to go.

  • Related