Linq To Entities - How to create a query where the table name is a parameter - entity-framework

Dynamic queries are not dynamic enough. I have seen solutions like this but still I have to indicate which table to use as the basis:
var query = db.Customers.Where("...").OrderBy("...").Select("...");
I want to create a simple query tool where the user will select from available tables using a drop-down list. As the result, I want to show first few records. Therefore, I need to change the table too! That is, I need something like this:
string selectedTable = "Customers";
var [tableName] = SomeTypecastingOperations(selectedTable);
var query = db.[tableName].Where("...").OrderBy("...").Select("...");
Is EF dynamic enough to handle this?

Linq-to-entities doesn't support that. You can achieve that with Entity SQL or some ugly code which will have conditional logic for every set you want to query (like a big switch for table names).

Related

Is there a possible way to search the name of a lookup fields using the search.query using apex in salesforce?

I had a lookup field on my object and I want to search the name of the lookup using search.query in apex, how can I do this?. In my debug, I am returning nothing. Below is my sample code.
String query = 'FIND \'Sonny McNeil\' IN ALL FIELDS RETURNING ASPHPP__ASPayment_Source__c (Id, Name,ASPHPP__PPContact__r.Name), Contact';
System.debug('query '+query);
List<List<sObject>> searchRecords = Search.Query(query);
System.debug('searchRecords '+searchRecords);
return searchRecords.get(0);
I've found a solution to my problem, it's not possible to use SOQL to search such certain name of a lookup field, all I do is use a sample SQL and add some LIKE condition on my sql and query it to the database and return

This function can only be invoked from LINQ to Entities. How can I solve this?

I can't figure out a way to overcome this, I'm trying to select one item on a dropdown list and on the next one show only the items associated to what I selected previously.
var xyz = ViewBag.ID_Cliente = new SelectList(db.Clientes, "ID", "Num_Cliente");
var results = from e in db.Extintores.AsEnumerable()
where SqlFunctions.StringConvert((double)e.ID_Cliente).Contains(xyz)
select e.Num_Extintor.ToList();
ViewBag.ID_Extintor = new SelectList(results);
Can someone help me?
Based on your desired action, I would suggest using the fluent Linq syntax and composing your query based on whether a selection has been made or not:
Your "xyz" variable is confusing, what is this meant to hold? From your description it sounds like you would want to have a list of clients, then select a Client ID and use that client ID to filter these Extintores.
var selectedClientId = ViewBag.SelectedClientId;
var results = db.Extintores;
if (selectedClientId != null)
results = results.Where(x => x.ID_Cliente == selectedClientId);
ViewBag.Id_Extintor = new SelectList(results.Select(x => x.Num_Extintor));
Frankly using ViewBag to relay state between view and controller isn't recommended. ViewBag is dynamic, so while it is flexible, it's difficult to assert that the state between requests will be reliable. Instead you should leverage a (view)model between calls. I would look to preserve the data type for your variables in the view bag as much as possible. The "list" of available items (SelectList) may be a SelectList of strings, but when one is selected, it can be parsed back to the appropriate data type rather than casting to strings in the Linq Queries. The big problem with that casting approach is that you forgo the use of indexes in your database. So if your ViewBag has a list of ID_Clients, selecting one client from that list should mark that one SelectedClient ID so we can use that to filter the next level. If you want to leverage a multi-select then you need to store a collection of IDs.

Maximo: Save a dynamic query?

In Maximo 7.6.1.1, I can run a query on work orders with this WHERE clause: reportedby = :user:
When I hit Find the query runs successfully; it filters the records using the currently logged in user.
However, when I go back and look at the WHERE clause, I see that the dynamic variable (:user) has been replaced with a static value (JSMITH).
And when I look at the query table in Toad, I see that the clause has been saved as reportedby = 'JSMITH'.
This is not what I want.
How can I save a query with a dynamic variable?
(Oracle 12c)
You can use the View/Manage Queries dialog to change it back. When you first run it Maximo replaces the bind variables.

Update one property's value to the value of another property in the same document

How can we replicate the following sql query in MongoDb?
update Person set Alias = Name;
where the Person table has the columns Alias, Name
I want the query to affect multiple rows. It doesn't matter if the update query cannot support upserts, I only need to update & not insert.
Unfortunately, that functionality is not available in MongoDB. You will need to loop through the documents, updating them one-at-a-time, and doing a read-update pair.
If you want this to be concurrency-safe, you'll need to implement some sort of locking; either optimistic or pessimistic.
myDb.myCollection.find({}).forEach(
function (person) {
person.Alias = person.Name
myDb.myCollection.save(person)
}
)

View from dynamically generated list of tables

So I basicly have a table which has a list of table names. All these listed tables have exact same structure.
Then I have a query template, with place holder for table name.
I need to create a view, which should return results of that query UNIONed from all the tables listed in that one setup table.
So far what I've done is create a user defined function, which would prepare a complete UNIONed SQL statement.
But this is where I'm stuck. I can't figure out how to execute it in a view and return whatever it returns..
My function returns SQL syntax.
I figured that UDF can't executed dynamic sql, so my method won't work. So far I've solved my issue at hand by generating views. But I would still prefer a more dynamic way..