2
$\begingroup$

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?

$\endgroup$
4
  • $\begingroup$ Since "Vancuver" is typed by "City" in this relational database there is simply no need for this. $\endgroup$ Commented May 28, 2020 at 20:03
  • $\begingroup$ 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. $\endgroup$
    – Daniele F
    Commented May 28, 2020 at 20:49
  • 1
    $\begingroup$ 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]. $\endgroup$
    – WReach
    Commented May 28, 2020 at 23:52
  • $\begingroup$ It's true it seems you're right. My mistake. Thanks! $\endgroup$
    – Daniele F
    Commented May 29, 2020 at 8:45

2 Answers 2

2
$\begingroup$

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
]
$\endgroup$
2
  • $\begingroup$ FYI, the OP has rewritten the question such that your response no longer applies. $\endgroup$
    – WReach
    Commented May 28, 2020 at 23:55
  • $\begingroup$ @WReach Thanks for letting me know. I hate when this happens, feel like a fool :). Anyway, I will keep it around for the time being. $\endgroup$ Commented May 29, 2020 at 15:02
0
$\begingroup$

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.