1

I am new to c# with linq and I am trying to figure out how to do multiple filtering selection with buttons.

I am connected to a database, I have enum stats{approved, rejected, pending approval, pending rejected, cancelled};

A IEnumerator type List, how do I do in such a way that the code can be more manageable as one can keep adding N number of choices in the future and it will still work rather than using if else or switch cases to hard code all possible cases.

e.g.

if(stats contains approve)
List = List.dbset.where(x => x.id.stats == approved);

//return the view that displays all the list of things with stats approved.

Am not sure how am I going to combine it into a loop. Require some assistance.

Desired output is I can select 2 or more buttons and the list returns those selected filter values element out.

DatB
  • 51
  • 6
  • You can create a list of values selected by the buttons and then use the list in query as `List.dbset.where(x => choices.Containts(x.id.stats));` here `choices` is the collection of values selected by buttons. – Chetan Oct 15 '19 at 01:49
  • @Bonbon, first of all don't use `enum` if you want to manage `stats`. I would suggest create another `object` `(class)` to define your `stats` with properties. The `stats` object should query from a database which contains all your stats and retrieve the records via `LINQ`. For example `IList statList = new List();` You can loop through from the `statList object` and do your logic there. – Junius Oct 15 '19 at 01:51
  • I'll give it a try! Thanks! – DatB Oct 15 '19 at 03:44
  • It is easy i linq to do x.stats = myVariable. Its hard to do x.(unknown) = y.unknown. If you are trying to do something like that you will need to build up the query dynamically using something like a predicate builder. I hope that helps – JKerny Oct 15 '19 at 04:18
  • Perhaps you are looking for Join patterns here: https://stackoverflow.com/a/3868608/13131 – Richard Anthony Hein Oct 15 '19 at 05:51
  • @ChetanRanpariya I have the choices already but due to the type I cannot use the contains method. – DatB Oct 17 '19 at 06:12
  • @Juniuz there is no need for another object class since I can loop through enums too. – DatB Oct 17 '19 at 06:13
  • 1
    @JKerny I already have some ways to get the filter but not sure how to combine them to make it flexible for any amounts of filtering buttons selected. I have a hashset that contains the stats, I have a List of application list that I can simply do x => x.stats.Equals(stats.cancelled) something like that. – DatB Oct 17 '19 at 06:13
  • 1
    @RichardAnthonyHein thanks for the suggestion but join patterns are not what I am looking for. – DatB Oct 17 '19 at 06:13
  • add `using System.Linq;` at the top of of your class file. What is the type of `choices` variable? What is the type of `x.id.stats` ? – Chetan Oct 17 '19 at 06:13
  • type of choices is enum and x.id.stats is also enum lol basically I am trying to filter with the enums given. So there is no point to make extra stuff to go about it. Either I try to find a way to combine different base case list into 1 and output it or I try to loop the list, if stats == approved i add to a list, and if more stats are selected then keep adding them to the list and output. – DatB Oct 17 '19 at 06:39

2 Answers2

0

Maybe you can develop a filter method it like this, than can send some values to filter ;

List<yourType> filteredList = dbset.ToList();
if(stats1 contains approve){
    List<yourType> filteredList = filteredList.where(x => 
x.id.stats==approved).ToList();
}
if(stats2 contains approve){
    List<yourType> filteredList = filteredList.where(x => 
x.id.stats==approved).ToList();
}
return filteredList;
OmerAkgun
  • 34
  • 3
  • I already have something like this, I have currently 5 List. 1 List that contains everything. 1 List that filters out each type of stats individually. I am trying to see how I can combine them all. The idea is that I am trying to combine my list into 1 list to display the output for example I selected stat 1 and 2, then I union list1 and list2 and output the list. Maybe I am restricting myself too much since the source code is not by me and I am tasked to implement new things with it so I guess I have to ask what can I touch and not touch. – DatB Oct 17 '19 at 06:14
  • All of lists are the same type ? if theese are the same type , you can addRange all choosed lists to filteredList variable to response ? – OmerAkgun Oct 17 '19 at 11:18
0

Okay so actually the problem is because the return views always return the list with everything despite multiple filtering. All I did was create a new displaylist and union every filtered list when filter is applied and return that displaylist

DatB
  • 51
  • 6