Pass table name a parameter to the Data Context method using LINQ to SQL in C# - c#-4.0

I have created a method which delete data from the database by joining a db column with a csv file, I have a list of tables in a Data Context which I need to iterate, i means that I need to change the table name by passing it as parameter to the method.
Basically I need to use this method for all the table present in the Data Context
Current hard coded Table name in a GetTable method: dw_Job
DataContext Object: job
Here is my code
var query = from line in File.ReadAllLines(file, Encoding.GetEncoding(1252))
//let jobrecord = line.Split(',')
join j in job.GetTable<dw_Job>() on
Convert.ToInt32(line.Split(',').GetValue(factColPos))
equals j.JobID
select j;
foreach (var deletejob in query)
{
Console.WriteLine(deletejob);
job.dw_Jobs.DeleteOnSubmit(deletejob);
}
job.SubmitChanges();
I can able to provide if you need any further details of my code. Please correct me on this
Thanks in advance

Related

Android Studio Room query to get a random row of db and saving the rows 2nd column in variable

like the title mentions I want a Query that gets a random row of the existing database. After that I want to save the data which is in a specific column of that row in a variable for further purposes.
The query I have at the moment is as follows:
#Query("SELECT * FROM data_table ORDER BY RANDOM() LIMIT 1")
fun getRandomRow()
For now I am not sure if this query even works, but how would I go about writing my function to pass a specific column of that randomly selected row to a variable?
Ty for your advice, tips and/or solutions!
Your query is almost correct; however, you should specify a return type in the function signature. For example, if the records in the data_table table are mapped using a data class called DataEntry, then the query could read as shown below (note I've also added the suspend modifier so the query must be run using a coroutine):
#Query("SELECT * FROM data_table ORDER BY RANDOM() LIMIT 1")
suspend fun getRandomRow(): DataEntry?
If your application interacts with the database via a repository and view model (as described here: https://developer.android.com/topic/libraries/architecture/livedata) then the relevant methods would be along the lines of:
DataRepository
suspend fun findRandomEntry(): DataEntry? = dataEntryDao.getRandomRow()
DataViewModel
fun getRandomRecord() = viewModelScope.launch(Dispatchers.IO) {
val entry: DataEntry? = dataRepository.findRandomEntry()
entry?.let {
// You could assign a field of the DataEntry record to a variable here
// e.g. val name = entry.name
}
}
The above code uses the view model's coroutine scope to query the database via the repository and retrieve a random DataEntry record. Providing the returning DataEntry record is not null (i.e. your database contains data) then you could assign the fields of the DataEntry object to variables in the let block of the getRandomRecord() method.
As a final point, if it's only one field that you need, you could specify this in the database query. For example, imagine the DataEntry data class has a String field called name. You could retrieve this bit of information only and ignore the other fields by restructuring your query as follows:
#Query("SELECT name FROM data_table ORDER BY RANDOM() LIMIT 1")
suspend fun getRandomRow(): String?
If you go for the above option, remember to refactor your repository and view model to expect a String instead of a DataEntry object.

Where is my error with my join in acumatica?

I want to get all the attributes from my "Actual Item Inventry" (From Stock Items Form) so i have:
PXResultset<CSAnswers> res = PXSelectJoin<CSAnswers,
InnerJoin<InventoryItem,
On<CSAnswers.refNoteID, Equal<Current<InventoryItem.noteID>>>
>
>.Select(new PXGraph());
But, this returns me 0 rows.
Where is my error?
UPDATED:
My loop is like this:
foreach (PXResult<CSAnswers> record in res)
{
CSAnswers answers = (CSAnswers)record;
string refnoteid = answers.RefNoteID.ToString();
string value = answers.Value;
}
... but i can not go inside foreach.
Sorry for the English.
You should use an initialized graph rather than just "new PXGraph()" for the select. This can be as simple as "this" or "Base" depending on where this code is located. There are times that it is ok to initialize a new graph instance, but also times that it is not ok. Not knowing the context of your code sample, let's assume that "this" and "Base" were insufficient, and you need to initialize a new graph. If you need to work within another graph instance, this is how your code would look.
InventoryItemMaint graph = PXGraph<InventoryItemMaint>.CreateInstance<InventoryItemMaint>();
PXResultset<CSAnswers> res = PXSelectJoin<CSAnswers,
InnerJoin<InventoryItem, On<CSAnswers.refNoteID, Equal<Current<InventoryItem.noteID>>>>>
.Select(graph);
foreach (PXResult<CSAnswers> record in res)
{
CSAnswers answers = (CSAnswers)record;
string refnoteid = answers.RefNoteID.ToString();
string value = answers.Value;
}
However, since you should be initializing graph within a graph or graph extension, you should be able to use:
.Select(this) // To use the current graph containing this logic
or
.Select(Base) // To use the base graph that is being extended if in a graph extension
Since you are referring to:
Current<InventoryItem.noteID>
...but are using "new PXGraph()" then there is no "InventoryItem" to be in the current data cache of the generic base object PXGraph. Hence the need to reference a fully defined graph.
Another syntax for specifying exactly what value you want to pass in is to use a parameter like this:
var myNoteIdVariable = ...
InventoryItemMaint graph = PXGraph<InventoryItemMaint>.CreateInstance<InventoryItemMaint>();
PXResultset<CSAnswers> res = PXSelectJoin<CSAnswers,
InnerJoin<InventoryItem, On<CSAnswers.refNoteID, Equal<Required<InventoryItem.noteID>>>>>
.Select(graph, myNoteIdVariable);
foreach (PXResult<CSAnswers> record in res)
{
CSAnswers answers = (CSAnswers)record;
string refnoteid = answers.RefNoteID.ToString();
string value = answers.Value;
}
Notice the "Required" and the extra value in the Select() section. A quick and easy way to check if you have a value for your parameter is to use PXTrace to write to the Trace that you can check after refreshing the screen and performing whatever action would execute your code:
PXTrace.WriteInformation(myNoteIdVariable.ToString());
...to see if there is a value in myNoteIdVariable to retrieve a result set. Place that outside of the foreach block or you will only get a value in the trace when you actually get records... which is not happening in your case.
If you want to get deep into what SQL statements are being generated and executed, look for Request Profiler in the menus and enable SQL logging while you run a test. Then come back to check the results. (Remember to disable the SQL logging when done or you can generate a lot of unnecessary data.)

In pentaho dashboard how to display the table component columns using different sql datasources

I want to display a table with data coming from different sql tables, like for a single table component, some columns of the table come from one sql query and other column from next query. Using join queries or sub queries will display wrong data. In table component there is only one option for selecting the data-source.
Is there any way to do this?
With Javascript, in the pre-Execution function of the table component, you can change the datasource, but not for single columns.
You can hide some columns in the table component if you want.
How to change the datasource with Javascript
I created a Simple Parameter: refresh_table, and it contains the datasource of the table: sql_Empty, to show an empty table.
I created some Select components and when you choose a value and then you click on the Button component (a search button), the search button changes the value in the simple parameter 'refresh_table' with a new datasource based on the value in the select component: sql_A, sql_B.
function a() {
var a = this.dashboard.getParameterValue('selectA');
var b = this.dashboard.getParameterValue('selectB');
if (a === 'ciao' && b === 'ciao') {
this.dashboard.fireChange('refresh_table', 'sql_A');
} else {
this.dashboard.fireChange('refresh_table', 'sql_B');
}
}
In the preExecution function of the table component, I wrote this function to change the datasource based on the parameter value of refresh_table.
function b() {
this.chartDefinition.dataSource = this.dashboard.getParameterValue('refresh_table');
}
I think you can do it with Pentaho Data Integration/Kettle/Spoon and then you can pass the results to the table component, but I don't know how.

Convert AutoQuery query string to SqlExpression

I am trying to re-create AutoQuery queries outside of a service request. I am doing this because I give user option to save a request and then use that data elsewhere. I save the query string data so I am trying to create a query from the saved query string.
I need 2 things.
1) query that returns the complete data not limited by default autoquery page size
2) query that returns the count
I tried making the query like this:
IAutoQueryDb _autoQuery = HostContext.TryResolve<IAutoQueryDb>();
var dto = new MyQueryDbClass();
Dictionary<string, string> pars = GetParameters();
var query = _autoQuery.CreateQuery(dto, pars);
The problem with this is that the query generated has the table name of the response object and not the actual table so it doesn't work. Also I am unable to call ToCountSatement() on it. It is also limited by my default page size.
Is there a way to convert the AutoQuery query string to a SqlExpression so I can execute it and also get count statement?
The CreateQuery() API returns a populated SqlExpression<Table> similar to what would have been created if manually constructing the query yourself, e.g:
SqlExpression<Table> query = _autoQuery.CreateQuery(dto, pars);
To clear the paging info you can call .Limit() without arguments which will clear any populated Offset/Rows values:
query.Limit();
The Custom AutoQuery Implementations docs shows an example of AutoQuery executes the query behind the scenes, e.g. you can get the total with:
var total = Db.Count(query);

Entity Framework 4.1 & existing database

Hi I have an existing database with a table with 30 fields, I want to split the table into many models so I could retrieve/save fields that I need and not every time retrieve/save the whole object from the db. using c#.
I think I should be using Code-First. Could someone provide an example or a tutorial link?
thanks,
You don't need to split table to be able to load a subset of field or persist subset of fields. Both operations are available with the whole table mapped to single entity as well.
For selection you simply have to use projection:
var data = from x in context.HugeEntities
select new { x.Id, x.Name };
You can use either anonymous type in projection or any non-mapped class.
For updates you can simply use:
var data = new HugeEntity { Id = existingId, Name = newName };
context.HugeEntities.Attach(data);
var dataEntry = context.Entry(data);
dataEntry.Property(d => d.Name).IsModified = true; // Only this property will be updated
context.SaveChanges();
Or:
var data = new HugeEntity { Id = existingId };
context.HugeEntities.Attach(data);
data.Name = newName;
context.SaveChanges(); // Now EF detected change of Name property and updated it
Mapping multiple entities to single table must follows very strict rules and it is possible only with table splitting were all entities must be related with one-to-one relation (and there are some problems with more than two entities per split table in code first) or with table-per-hierarchy inheritance. I don't think that you want to use any of them for this case.

Resources