How to initiate a var which will store array of values with different datatypes? - entity-framework

I have a dynamic expression built in .net, which works if the column type is string, however, if the column is date or int then i have to convert the input to the respective datatype before passing to the expression. How can I make it dynamic?
MemberExpression member = Expression.Property(param, filter.Name);
var propertyType = ((PropertyInfo)member.Member).PropertyType;
if (propertyType.GetType() == typeof(Int32))
{
var criteria = Array.ConvertAll(filter.Value, Int32.Parse);
}
else if (propertyType.GetType() == typeof(DateTime))
{
var criteria = Array.ConvertAll(filter.Value, DateTime.Parse);
}
else
{
var criteria = filter.Value;
}
criteria.Aggregate( // -- gives error here since criteria does not exist in current context
(Expression)Expression.Constant(false),
(acc, next) =>
Expression.MakeBinary(
ExpressionType.Or,
acc, Expression.Equal(member, Expression.Constant(next))));
I already tried defining
var criteria = null;
var criteria = object[];
dynamic criteria;
var criteria = new {};
Payload
{
"pageFilters": [
{
"name": "string",
"condition": "Equals",
"value": [
"string"
]
}
]
}

Related

how to filter entity type in Dart/Flutter?

I got an array of two items. I want to filter the array and return the item only if fromType matches EntityType.producerSite .
var transferlist = [Transfer($id: 62c57c7810a57a59fe1e, fromType: EntityType.producerSite, fromId: 629f5ffd2b6386c83057, toType: null, timestamp: 2022-07-06 13:13:44.016), Transfer($id: 62c57c7810a57a59fe1e, fromType: EntityType.bin, fromId: 629f5ffd2b6386c83057, toType: null, timestamp: 2022-07-06 13:13:44.016)]
I try this but it didn't work
var newTransferList = []
for (var transfer in transferlist) {
if (transfer.fromType == "EntityType.producerSite") {
newTransferList.add(transfer)
}
}
You can use where, your main issue is you put EntityType.producerSite inside " " :
var result = transferlist.where((element) => element.fromType == EntityType.producerSite).toList();
or if you want it in for loop form:
for (var transfer in transferlist) {
if (transfer.fromType == EntityType.producerSite) {
newTransferList.add(transfer)
}
}

Build predicates for a postgres jsonb column with criteria builder exact match using JPA criteria

private void teamsCriteria(Root<Employee> root, CriteriaBuilder criteriaBuilder, List<Predicate> predicates) {
var teamsPredicateArr = new Predicate[filters.getTeams().size()];
for (var i = 0; i < filters.getTeams().size(); i++) {
teamsPredicateArr[i]=criteriaBuilder.like(criteriaBuilder.concat(root.get(teams), \\:\\:text), "%" + filters.getTeams().get(i) + "%");
}
var predicate = criteriaBuilder.or(teamsPredicateArr);
predicates.add(criteriaBuilder.and(predicate));
}
Example: I have jsonb column teams
{
"team": [
"DEFAULT"
]
}
{
"team": [
"EF"
]
}
If I execute above code I am getting both the teams
I want exact match of jsonb column value:
Expected result :I have to filter only "EF"

Search field: Filtering nested object

Hi all,
I have the following data structure :
[ {
"supplierCode": "supplier1",
"supplierDesc": "supplier1Desc",
"pos": [ {
"poNum": "11111",
"materialNum": "matNum11",
"materialDesc": "matDesc11"
},
{ "poNum": "11112",
"materialNum": "matNum22",
"materialDesc": "matDesc22"}
] },
{"supplierCode": "supplier2",
"supplierDesc": "supplier2Desc",
"pos": [ {
"poNum": "22222",
"materialNum": "matNum11",
"materialDesc": "matDesc11"},
{"poNum": "22223",
"materialNum": "matNum22",
"materialDesc": "matDesc22"}]
}
]
My task is to filter data in JSON model by properties in pos array.
I tried the following approach:
myList = this.getView().byId("myList");
var binding = myList.getBinding("items");
if (!query) {
binding.filter([]);
} else {
binding.filter([new sap.ui.model.Filter([
new sap.ui.model.Filter("supplierCode", sap.ui.model.FilterOperator.Contains, query),
new sap.ui.model.Filter("supplierDesc", sap.ui.model.FilterOperator.Contains, query),
new sap.ui.model.Filter("pos/materialDesc", sap.ui.model.FilterOperator.Contains, query)
], false)]);
}
with no luck.
Also, I found out it is possible to do with ODataModel, but I didn't find anything regarding JSONModel.
Can such filtering be done at all?
Thank you.
Here is an example on test function under the constructor of the Filter :
Filter result will return a line of list containing the materialDesc description introduced in the filter :
onFilterInvoices: function(oEvent) {
// build filter array
var aFilter = [];
var sQuery = oEvent.getParameter("query");
if (sQuery) {
aFilter.push(new sap.ui.model.Filter({
path: "pos",
test: function(oValue) {
var oMaterials = oValue;
for (var i in oMaterials) {
if (oMaterials[i].materialDesc === sQuery) {
return true;
}
}
return false;
}
}));
}
// filter binding
var oList = this.getView().byId("listapp");
var oBinding = oList.getBinding("items");
oBinding.filter(aFilter);
}

How to count objects inside object (without count var index)

I have the following model stored in mongodb:
{
"_id": (MongoId)
"user": "X",
"field-name-1":{
"obj-1": 12345,
"obj-2": 54321,
"obj-3": 67890,
(...)
},
"field-name-2:{
"obj-1": {"foo":"bar", "bar":"foo"},
"obj-2": {"foo":"bar2", "bar":"foo2"},
(...)
}
}
How do I find the ocurrences that contains more than 5 objects inside the "field-name-1"? Is it the same for "field-name-2"?
I can't use a variable to count the amount of objects because I upsert values inside the "field-name-1"/"field-name-2" constantly.
Thanks.
Already tried (without success):
db.summoners.find({$where: "this.['field-name-1'].length > 5"})
db.summoners.find({$where: "['field-name-1'].length > 5"})
$where should be qualified in this case:
function findMyDocument(key, value) {
var f = this[key];
if (typeof f != "object" || typeof value != "number") {
return false;
}
var count = 0;
for (var x in f) {
if (++count > value) {
return true;
}
}
return false;
}
// store above function to database for invoking
db.system.js.insert({_id:"findMyDocument", value: findMyDocument});
// apply here
db.summoners.find({$where: "findMyDocument.call(this, 'field-name-1', 5);"});
db.summoners.find({$where: "findMyDocument.call(this, 'field-name-2', 5);"});

Create a tag for generating #link in Play2.0

while actively learning Play2.0 I am stuck with creating a tag. In the sample application, called computer-database, the following helper is created in the list template:
#****************************************
* Helper generating navigation links *
****************************************#
#link(newPage:Int, newSortBy:String) = #{
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
controllers.orders.routes.Work.list(newPage, sortBy, order, currentFilter)
}
Since I want to use this helper in a view templates I thought that the best solution would be to create a tag for it. So I did the following (in my tags package):
#(newPage : Int, newSortBy:String) {
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
controllers.orders.routes.Computer.list(newPage, sortBy, order, currentFilter)
}
But, obviously this is not working and I do not know where or why it is not working.
Thanks for the input.
UPDATE WITH ANSWER:
So in Scala template we have to define, just as in Java, the arguments that are passed to this view (Note: that the variables that you will use in the javascript must be passed too!). The template will be compiled as a method as stated in the documentation.
The working tag looks like:
#(newPage : Int, newSortBy : String, currentSortBy: String, currentOrder: String, currentFilter : String ) #{
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
controllers.orders.routes.Work.list(newPage, sortBy, order, currentFilter)
}
The trick is that the first version uses a template syntax allowing to write Scala code instead of HTML: #{ val scalaVal = 42}.
In your tag, the template engine interpretes your code as HTML.
If you want to copy-paste this code, don’t forget the leading # before the opening brace.