I have a piece of code which returns a Sales Order from AX. In that record im using the querySalesLine method but I'm not sure where I go from there to get all the lines attached to the order below is my code:
AxaptaRecord OrderRecord = (AxaptaRecord)ax.CallStaticClassMethod("OnlineOrder", "getSalesOrder", salesRef);
if(OrderRecord.Found)
{
AxaptaObject Lines = (AxaptaObject)OrderRecord.Call("querySalesLine");
}
How would I then use this Lines object to retrieve all of the items attached to this order? I know that the querySalesLine returns a Query object but not sure what to do next.
You should create a QueryRun object, then use that object to read the lines.
var qLines = (AxaptaObject)OrderRecord.Call("querySalesLine");
var qrLines = ax.CreateAxaptaObject("QueryRun", qLines);
To read the lines use this answer.
The Query is a static description of the query.
The QueryRun uses the query to find the records.
Related
I'm trying to build a database using SQLiteAsyncConnection where all of it's queries comes from the server.
database = new SQLiteAsyncConnection(dbPath);
The server's queries are divided into two parts[execution & retrieval]
for the execution queries I'm using this line of code
database.ExecuteAsync(serverResponse);
where serverResponse may be a sql CREATE, INSERT or UPDATE statement.
As for the retrieval queries I'm trying to find a way to retrieve dynamic objects from the database and serialize them to send them to the database. I'm trying to make something like this to retrieve the list of objects in database
var ls = await database.QueryAsync<dynamic>(serverResponse);
but this statement returns a List of objects, and I don't know how to use it.
I tried something like this locally
database.ExecuteAsync("CREATE TABLE Persons(PersonID int);");
database.ExecuteAsync("INSERT INTO Persons(PersonID) VALUES (5);");
var ls = await database.QueryAsync<dynamic>("SELECT * FROM Persons");
int x = ls[0].PersonID;
But it gives me this error 'object' does not contain a definition for 'PersonID'; which says that I need to cast the object to a Person class to be able to use it.(Note that I don't have any class called Person that has a public property PersonID; objects are dynamic and I can't tell what the server's query are).
I tried to serialize ls
string str = JsonConvert.SerializeObject(ls);
but it returns a JSON array with empty object [{}].
How can I serialize the result of any query without the need to cast it.
var ls = await database.QueryAsync<object>("SELECT * FROM Persons");
var json = JsonConvert.SerializeObject(ls);
try this ?
Hy,
I have a csv file like that
user_name1,c:\photo\user_photo.jpg,0,0,0,0
in which every line refers a distinct object with own fileds separated from comma.
How I find a particular object knowing the user name? I use distinct user_names. And after that how I make that object, curent object that I use?
What i have done until now:
StreamReader sc = new StreamReader(#"C:\Player.csv");
String linie = sc.ReadLine();
while (!String.IsNullOrEmpty(linie))
{
string[] stringu = linie.Split(',');
Player player = new Player(stringu[0], stringu[1], int.Parse(stringu[2]), int.Parse(stringu[3]), int.Parse(stringu[4]));
players.Add(player);
linie = sc.ReadLine();
}
sc.Close();
var query = players.Where(a => a.Name == label6.Text);
}
Sincerly,
You could try to use a library, which makes easy the use of CSV files with LINQ queries. Please look here.
Firstly I'd suggest that for parsing a CSV file you don't roll your own field splitter. I'd suggest using something like the TextFieldParser (which can be used in C# by referencing Microsoft.VisualBasic).
Once you've created the parser you can use it to get the array of strings for each record represented by line in your application:
List<Players> players = new List<Players>();
var parser = new TextFieldParser();
while ( !parser.EOF )
{
string [] playerFields = parser.ReadFields();
// Create player from fields
var player = Player.FromFields(playerFields);
players.Add(player);
}
Now it really depends on whether you want to continuously query the players in the file, as if you do then getting an in-memory copy and using LINQ makes sense, else if you only want to query once then I'd simply do the check line by line.
Assuming that you do want to query multiple times then parsing the file and holding the values in a List or similar makes sense (assuming the file isn't ridiculously big).
Finally, if you has distinct user names then you could use the FirstOrDefault method in LINQ to give you the single player back that matches.
var player = players.FirstOrDefault(p => p.Name.Equals(textBox.Text));
Or if you know that you have unique player names you could just store the whole lot in a dictionary....?
if ( players.ContainsKey(textBox.Text) )
{
var player = players[textBox.Text];
}
Anyway, just some thoughts.
The only problem you have at the moment, as far as I can see, it that you say you are looking to fetch "a particular object", but in your last line you are using Where which is going to return an IEnumerable containing one, none or many such objects. If you want to fetch one uniquely named object you should use Single or SingleOrDefault, e.g.
var myPlayer = players.Single(x => x.Name == label6.Text);
The answer you have accepted has merely converted your query from method syntax to the equivalent query syntax and introduced a compilation error by attempting to convert the resulting IEnumerable<Player> to a Player.
In anticipation of a problem you may be about to have, you might also want to look into Microsoft.VisualBasic.FileIO.TextFieldParser (you'll need to add a reference to Microsoft.VisualBasic.dll), which will allow you to parse a CSV in a more robust way, i.e. handle fields containing commas etc., using something like the following:
using (var parser = new TextFieldParser(filename))
{
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
var p = parser.ReadFields();
players.Add(new Player(p[0], p[1], int.Parse(p[2]), int.Parse(p[3]), int.Parse(p[4])));
}
}
Have you tried something like
Player selected_player = from pl in players
where pl.Name == label6.Text
select pl;
?
I have a method that I want to pull back data from a sharepoint list. In the method I have the following code:
var listPath = new Uri(string.Format("{0}/forms/", SitePath));
var fieldNames = new List<string> {"Id","UserName", "FullName"};
var query = XElement.Parse(#"<Query/>");
var listData = ListServiceUtility.GetListItemData(listPath, listName, string.Empty, fieldNames, query, false, 50);
when I break out listData I only have data for FullName, I don't have the other fields I wanted that I have in fieldNames.
I have not found much in the way of examples in C# and the VB example only makes a call for 1 field. Kind of pointless if I cant bring back more than one field in a list.
Am I using the wrong method? How can I get more than one field in my collection of data?
Any help would be appreciated.
Hi i am trying to get to grips with Dapper.
My situation is i want to pull two values from a query into two separate strings. Im not sure if i am going about this in the correct way, but this is what i am doing:
string sql = #"Select type, name
FROM ZipData
WHERE Zip = #zip";
using (var multi = conn.QueryMultiple(sql, new { zip = zip }))
{
string result = multi.Read<string>().SingleOrDefault();
}
And i am getting Cannot access a disposed object. Object name: 'GridReader'. when trying to read the second string.The thing is it gets the first value correctly and has both the fields in in the reader i am trying to get. Im sure im misusing the api.
What am i doing wrong here? Ive googled but can find a specific example.
You are mis-using QueryMultiple. That is defined for compound SQL statements that return multiple result sets. Something like:
SELECT Foo FROM MyTable;
SELECT Bar FROM MyOtherTable;
On the other hand, you are trying to get two different columns from a single result set, so you should just use the normal Query method:
var result = conn.Query(sql, new { zip = zip }).Single();
var type = result.type;
var name = result.name;
Query returns an enumerable (because generally a query can return multiple rows). It appears that you only want one row, however, so we invoke .Single at the end to just get that row. From there, the return type is dynamic so you can simply refer to the properies implied by the columns in your SELECT statement: type and name.
I'm trying to find, then update, a specific DataRow in a DataTable. I've tried a few things based on my searches, and the code below seems to be the closest I can get. The linq will return one row. With that row, I'd like to update column values (Status, StopTime, Duration). I can't for the life of me find how to do this.. I've tried casting, but I'm new to linq and don't see how to update these values.
private DataTable downloadProcStatusTable;
void UpdateDataDownloadProcedureList(ProcedureStats ProcStats)
{
var currentStatRow = from currentStat in downloadProcStatusTable.AsEnumerable()
where currentStat.Field<String>("ProcedureName") == ProcStats.ProcName
select currentStat;
}
Your query as it stands actually gives you an IEnumerable<DataRow>. You need to do this to get the actual row:
var currentStatRow = (from currentStat in downloadProcStatusTable.AsEnumerable()
where currentStat.Field<String>("ProcedureName") == ProcStats.ProcName
select currentStat).SingleOrDefault();
You should then be able to use the currentStatRow variable to modify the column values.
Outline
Load the existing entity from the database (unless you have one that you can re-attach, in which case you could avoid this additional query)
Update the properties as needed
Submit the changes back to the database using SubmitChanges()
Implementation
I wasn't exactly sure where your variables are and the names, but this should give you a good start...
void UpdateDataDownloadProcedureList(ProcedureStats ProcStats)
{
var currentStatRow = (from currentStat in downloadProcStatusTable.AsEnumerable()
where currentStat.Field<String>("ProcedureName") == ProcStats.ProcName
select currentStat).FirstOrDefault();
currentStatRow.Status = ProcStats.Status;
currentStatRow.StopTime = ProcStats.StopTime;
currentStatRow.Duration = ProcStats.Duration;
downloadProcStatusTable.SubmitChanges();
}