the problem is quite simple. We have some store with many products.
For example each item has fields (and many others):
Height
Weight
Length
Name
Color
Price
We implement some page where we can filter items by range for numbers (like price, length) and string (color).
So, the problem is here:
- we need to allow people to filter items by any of criteria above (color+price, color+length+weight).
The basic approach with predefined SELECT+WHERE is too hard to main.
Is there any other option?
Thank you.
Jonathan is right. I think using if cases would be easiest and would look something like this.
var res = _dbContextorSource.table.Where(x => x.ID > 0); //something to get all of them
//or (x => x.ID >= 0 && x.ID <= 50) if you're showing 50 on a page
if(FirstFilterIsUsed){
res = res.table.Where(x => x.FirstField == FirstFilter);
}
if(SecondFilterIsUsed){
res = res.table.Where(x => x.SecondField == SecondFilter);
}
//etc
I think you could implement a more clean solution that loops through each filter. This is super pseudo but I've used solutions like this.
var filters = GetUserFilters();
foreach(Filter filter in filters){
res = res.table.Where(x => x.GetType().
GetProperty(filter.MatchingName).GetValue(x) == filter.FilterValue);
}
var result = res.ToList();
Common Solution
The most common solution is performing a big if` case. It takes some time to support all your fields but will work great at the end.
However, if you want to do it dynamically, 2 third-party libraries can help you with this
LINQ Dynamic
https://www.nuget.org/packages/System.Linq.Dynamic.Core/
The syntax required is a little bit different from C# but work great. It's the most popular library to do such a thing.
C# Eval Expression
Disclaimer: I'm the owner of the project C# Eval Expression
The library is not free, but you can do pretty much any dynamic LINQ using the same syntax as C#.
So you will be able to build a string to evaluate and the library will do the rest for you.
Here are some example using EF Classic:
https://dotnetfiddle.net/Otm0Aa
https://dotnetfiddle.net/mwTqW7
I have the following if statement:
var formatCity =
obj => R.both(has('city'), has('state'))(obj) ? appendCommaToCity(obj) : obj
I would like to make this code point free, but can not figure out a way around the if statement.
That's quite simple actually using the ifElse function - or its specialisation, when:
const formatCity = R.when(R.both(has('city'), has('state')), appendCommaToCity);
I'm working on a simple web service in Prolog and wanted to respond to my users with data formatted as JSON. A nice facility is reply_json_dict/1 which takes a dictionary and converts it in a HTTP response with well formatted JSON body.
My trouble is that building the response dictionary itself seems a little cumbersome. For example, when I return some data, I have data id but may/may not have data properties (possibly an unbound variable). At the moment I do the following:
OutDict0 = _{ id : DataId },
( nonvar(Props) -> OutDict1 = OutDict0.put(_{ attributes : Props }) ; OutDict1 = OutDict0 ),
reply_json_dict(OutDict1)
Which works fine, so output is { "id" : "III" } or { "id" : "III", "attributes" : "AAA" } depending whether or not Props is bound, but... I'm looking for an easier approach. Primarily because if I need to add more optional key/value pairs, I end up with multiple implications like:
OutDict0 = _{ id : DataId },
( nonvar(Props) -> OutDict1 = OutDict0.put(_{ attributes : Props }) ; OutDict1 = OutDict0 ),
( nonvar(Time) -> OutDict2 = OutDict1.put(_{ time : Time }) ; OutDict2 = OutDict1 ),
( nonvar(UserName) -> OutDict3 = OutDict2.put(_{ userName : UserName }) ; OutDict3 = OutDict2 ),
reply_json_dict(OutDict3)
And that seems just wrong. Is there a simpler way?
Cheers,
Jacek
Instead of messing with dictionaries, my recommendation in this case is to use a different predicate to emit JSON.
For example, consider json_write/2, which lets you emit JSON, also on current output as the HTTP libraries require.
Suppose your representation of data fields is the common Name(Value) notation that is used throughout the HTTP libraries for option processing:
Fields0 = [attributes(Props),time(Time),userName(UserName)],
Using the meta-predicate include/3, your whole example becomes:
main :-
Fields0 = [id(DataId),attributes(Props),time(Time),userName(UserName)],
include(ground, Fields0, Fields),
json_write(current_output, json(Fields)).
You can try it out yourself, by plugging in suitable values for the individual elements that are singleton variables in the snippet above.
For example, we can (arbitrarily) use:
Fields0 = [id(i9),attributes(_),time('12:00'),userName(_)],
yielding:
?- main.
{"id":"i9", "time":"12:00"}
true.
You only need to emit the suitable Content-Type header, and have the same output that reply_json_dict/1 would have given you.
You can do it in one step if you use a list to represent all values that need to go into the dict.
?- Props = [a,b,c], get_time(Time),
D0 = _{id:001},
include(ground, [props:Props,time:Time,user:UserName], Fs),
D = D0.put(Fs).
D0 = _17726{id:1},
Fs = [props:[a, b, c], time:1477557597.205908],
D = _17726{id:1, props:[a, b, c], time:1477557597.205908}.
This borrows the idea in mat's answer to use include(ground).
Many thanks mat and Boris for suggestions! I ended up with a combination of your ideas:
dict_filter_vars(DictIn, DictOut) :-
findall(Key=Value, (get_dict(Key, DictIn, Value), nonvar(Value)), Pairs),
dict_create(DictOut, _, Pairs).
Which then I can use as simple as that:
DictWithVars = _{ id : DataId, attributes : Props, time : Time, userName : UserName },
dict_filter_vars(DictWithVars, DictOut),
reply_json_dict(DictOut)
I have a question and maybe somebody can help me figure out what the best way would be to achieve the solution. What i wanted to do is the reverse of:
http://doc.silverstripe.com/framework/en/topics/datamodel#highlighter_745635
WHERE ("LastName" = 'Minnée' AND ("FirstName" = 'Sam' OR "Age" = '17'))
I want to get something along the lines of:
WHERE( ("LastName" = 'Minnée') OR ("FirstName" = 'Sam' AND "Age" = '17'))
Now i cannot find any way to achieve this effect seeing as i cannot add a filter within the filterAny
For now i am doing it with the get()->where( ... ) option but was more wondering with this question if their are alternative options without having to write normal SQL code.
As 'AND' has a higher priority it doesn't need to be in braces. You can just write it as:
WHERE( "LastName" = 'Minnée' OR "FirstName" = 'Sam' AND "Age" = '17')
But as far as I can tell on the first look there isn't a way to write this without using where(). Let us know if you find a way. For debuging you can display the generated SQL-Query by calling the function sql():
... get()->where( ... )->sql();
I have this code there Federal_Mandate I am checking weather this MandateType is 1 or 0
if its one I am just converting this as 1 or 0
mandate.Federal_Mandate = collection["MandateType"].ToString().Equals("Federal") ? Convert.ToByte(1) : Convert.ToByte(0);
and my datbase Federal_mandate datatype has tiinyint.
is that something doing wrong i am here.. why I am gettting object reference error here?
thanks
one of mandate, collection and collection["MandateType"] is null. Set a breakpoint and find out which.
It's pretty hard to figure it out but ... couldn't it be cause your collection["MandateType"] is null?
Maybe you can change it to something like this:
mandate.Federal_Mandate = (collection["MandateType"] ?? "").ToString().Equals("Federal") ? Convert.ToByte(1) : Convert.ToByte(0);
You need to check your collection to see if its null before calling a method on it:
mandate.Federal_Mandate = Convert.ToByte(0);
if(collection["MandateType"] != null)
{
mandate.Federal_Mandate = collection["MandateType"].ToString().Equals("Federal") ? Convert.ToByte(1) : Convert.ToByte(0);
}