2

I am a student and I am new of Mathematica. I am working with relational databases and I am following the guide "Relational Database quick start" find here: https://reference.wolfram.com/language/tutorial/RelationalDatabasesQuickStart.html

I have successfully managed to connect to my database. Now I am using the Entity Framework to query my database.

The problem is that I want to pass a global value inside the EntityFunction and not define the column name a priority. I would like to use the code inside a function and pass dynamic parameters to this function.

So this first example works.

EntityValue[
  FilteredEntityClass[
   "Customer",
   EntityFunction[e, e["City"] != "Vancuver"]
   ],
  {"FirstName"},
  "PropertyAssociation"
  ] // Dataset

But I want to do something like this

function[var_,value_] := (
 entity = EntityValue[
  FilteredEntityClass[
   "Customer",
   EntityFunction[e, e[var] != value]
   ],
  {"FirstName"},
  "PropertyAssociation"
  ] // Dataset;
 Return[entity];
);

res = function["City", "Vancouver"];

When I try this second solution I get this error

EntityFunction::invexpr: The expression e[var] cannot be compiled to SQL.

Is it not possible to do something like this?

Daniele F
  • 21
  • 3
  • Since "Vancuver" is typed by "City" in this relational database there is simply no need for this. – Steffen Jaeschke May 28 '20 at 20:03
  • I modified the code inside the question because perhaps I had not explained myself well. I would like to pass parameters in EntityFunction. In the code shown if I use only "value" as a parameter of the function everything goes correctly, but if I change it and I also have the parameter "var" I get the error shown. It seem that e[var] is not accepted by Mathematica. – Daniele F May 28 '20 at 20:49
  • 1
    Your updated example works for me. You might have some left-over earlier definitions of function that are interfering with its operation. Try re-evaluating its definition after evaluating ClearAll[function]. – WReach May 28 '20 at 23:52
  • It's true it seems you're right. My mistake. Thanks! – Daniele F May 29 '20 at 08:45

2 Answers2

2

Since EntityFunction, just like Function, holds its variable and body, you will have to inject the value into it. This is not specific to EntityFunction, and is a common problem of injecting values into held expressions. Just as in other such cases, you can use e.g. With to do that:

With[{var = "City"},
  EntityValue[
    FilteredEntityClass[
      "Customer",
      EntityFunction[e, e[var] != "Vancuver"]
    ],
    {"FirstName"},
    "PropertyAssociation"
  ] // Dataset
]
Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420
0

Most probable this

Dataset vw. Association

sheets sufficient help for the intend of the question. Since the answer there goes deeper than this question it hard to name this question a duplicate. It is too under question whether the shown competence on relational data handling from the questioner is sufficient to understand the answer suggested.

Aside from typing scoping is for sure a possible resolution, change of perspective to this question.

Steffen Jaeschke
  • 4,088
  • 7
  • 20