question

osyris-3187 avatar image
0 Votes"
osyris-3187 asked osyris-3187 commented

IQueryable Multiple filters on same item

I am working on a hotel website
the website has 5 Check buttons ranging from 1 Hotel Star to 5

if none of the checkbuttons are enabled/clicked it should show all hotels

if one of the the 5 checkbuttons are enabled/clicked it should only show the hotel with that hotel star

if multiple checkbuttons are enabled/clicked it should only show those hotels with those stars;


I tried doing something like this


code:

                 if (dto.Star1)
                     rooms = rooms.Where(x => x.Stars == 1);
    
                 if (dto.Star2)
                     rooms = rooms.Where(x => x.Stars == 2);
    
                 if (dto.Star3)
                     rooms = rooms.Where(x => x.Stars == 3);
    
                 if (dto.Star4)
                     rooms = rooms.Where(x => x.Stars == 4);
    
                 if (dto.Star5)
                     rooms = rooms.Where(x => x.Stars == 5);



If one of the Checkbuttons is clicked/enabled it works perfect but if I click enable mutliple checkbottons it returns nothing

Help me out with this please.

dotnet-aspnet-core-generaldotnet-aspnet-generaldotnet-aspnet-core-webapidotnet-entity-framework-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered osyris-3187 commented

you want to generate an in clause:

 var starList = new List<int>();
        
 if (dto.Star1) starList.Add(1); 
 if (dto.Star2) starList.Add(2); 
 if (dto.Star3) starList.Add(3); 
 if (dto.Star4) starList.Add(4); 
 if (dto.Star5) starList.Add(5); 
    
 var  query = rooms.Where(x => starList.Contains(x.Stars));
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

They both work great Thanks.

I was just wondering how your first code works.

What the "|" and "&" ment

because I like that code its short and simple

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

I cannot see your code but something like the following pattern.

 var results = DbContext.Rooms.Where(x => (x.Stars == 1 & dto.Star1) |
     (x.Stars == 2 & dto.Star2) | 
     (x.Stars == 3 & dto.Star3) | 
     (x.Stars == 4 & dto.Star4) |
     (x.Stars == 5 & dto.Star5)).ToList();
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

osyris-3187 avatar image
0 Votes"
osyris-3187 answered AgaveJoe commented

Thank you very much Joe,

I have tried to look up this problem for a couple of hours now,
Could you explain how it works im curious.

I know that "||" means OR, but what is "|"?
and what is a single & sign?

Is this a conditional operator inside a LINQ ?
I have never seen that but that would be pretty genius


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In C# a single | is a Boolean OR operator. A double || is a conditional OR operator. The same applies to & and &&. See the C# reference documentation.

Boolean logical operators (C# reference)

Could you explain how it works im curious.

It's basic Boolean logic. The following snippet is a logical AND. Both side of the Boolean AND (&) must be true to return a TRUE and fetch columns with one star. If dto.Star1 is false then FALSE is returned and no one star hotels are returned.

 (x.Stars == 1 & dto.Star1) 

The following snippet adds the Boolean OR. If the result on the left of the OR (|) is TRUE and the right side is FALSE then only one stars are returned. If both sides are true TRUE then one stars and two stars are returned. If the left side is FALSE and the right side is TRUE then two stars hotels are returned.

  (x.Stars == 1 & dto.Star1)  | (x.Stars == 2 & dto.Star2) 

Learn truth tables.
https://en.wikipedia.org/wiki/Truth_table










0 Votes 0 ·