Given the below code I want to be able to find an element in the myProjectsDicList using a key (string) in a different dictionary. But look for it in projectsArrayList array. Specifically the htmleditor.ProjectName string is what I want to find. There is only 1 List<ArrayList> in the dictionary. This array list contains the projects array list.
I tried this below but doesn't work....
string key = "Project Name";
var match = myProjectsDicList.Where(item => item.Value[0].Cast<object>().Where(x => x.ProjectName == key).Select(x => x.ProjectName).First());
I'm trying to find a way to replace this code to find the HTMLEditor that works with a select statement.
bool foundHTMLEditor = false;
for (int i = 0; i < myProjectsDicList.Count; i++)
{
List<ArrayList> allprojects = myProjectsDicList.ElementAt(i).Value;
for (int j = 0; j < allprojects[0].Count; j++)
{
HTMLEditor e = (HTMLEditor)allprojects[0][j];
if (e.ProjectName == key)
{
foundHTMLEditor = true;
break;
}
}
if(foundHTMLEditor == true)
{
break;
}
}
Here is the code I have...
public class MultiDimDictList : Dictionary<string, List<ArrayList>>
{
}
projects = new ArrayList();
List<ArrayList> projectsArrayList = new List<ArrayList>();
// create the dictionary
MultiDimDictList myProjectsDicList = new MultiDimDictList();
// create an htmleditor instance and set one of the variables
HTMLEditor htmleditor = new HTMLEditor();
htmleditor.ProjectName = "Project Name";
// add the htmleditor to the projects array list
projects.Add(htmleditor);
// add the projects array list to the List
projectsArrayList.Add(projects);
// add the projects array list to the dictionary list
myProjectsDicList.Add(domainName, projectsArrayList);
I don't know exactly what is the correct term/way to explain my question but i hope someone get my idea
My datatable are required to classify few type of sources (ex in image) and i need to calculate them according to the type.. is there any way i can perform this?
Thank you so much for your reply
Here's what i've tried so far
for (int l = 0; l < my_datatable.Rows.Count; l++)
{
data_source = my_datatable.Rows[l][3].ToString();
if (data_source.Contains("Cross Site Scripting"))
{
my_datatable.Rows[l][3] = "2";
}
else if (data_source.Contains("SQL Injection"))
{
my_datatable.Rows[l][3] = "3";
}
else if (data_source.Contains("Unicode Attack"))
{
my_datatable.Rows[l][3] = "4";
}
else if (data_source.Contains("Proxy Attack"))
{
my_datatable.Rows[l][3] = "5";
}
else
{
my_datatable.Rows[l][3] = "1";
}
current output
expected output
Code for CSV part
string[] raw_text =
System.IO.File.ReadAllLines("C:\\dummylog3.csv"); //Placement of the
.CSV Files
string[] data_col = null;
int x = 0;
foreach (string text_line in raw_text)
{
//MessageBox.Show(text_line);
data_col = text_line.Split(' ');
if (x == 0)
{
for (int i = 0; i <= data_col.Count() - 1; i++)
{
my_datatable.Columns.Add(data_col[i]);
}
x++;
}
else
{
my_datatable.Rows.Add(data_col);
}
my_datagridview.DataSource = my_datatable;
this.Controls.Add(my_datagridview);
}
Make a custom class which inherits the DataTable
public class CustomDataTable : DataTable
{
public enum ClassificationType
{
IMAGE
}
public ClassificationType classification { get; set; }
}
Make sure your project has a reference to System.Data.DataSetExtensions.dll, if not then add it. You will need this to call 'CopyToDataTable` extension method.
Create a table for your summarized data. We will just use this to create new rows for the summary table. But the data will not be stored here. See next point:
var summary = new DataTable();
summary.Columns.Add("Code");
summary.Columns.Add("Source Type");
summary.Columns.Add("Number Of Hits", typeof(int));
Using Linq, the code below will group the results by the Code and Source Type and then find the count of them. It will finally copy the data to a DataTable:
var result =
table.AsEnumerable()
.GroupBy(x => new { Code = x["Code"], SourceType = x["Source Type"] })
.Select(x => summary.Rows.Add(x.Key.Code, x.Key.SourceType, x.Count()))
.CopyToDataTable();
I am using Sqlite.Net in my Xamarin.Forms application. So far it has been great at returning lists of objects if my object is a class like so:
SqliteDatabase.Connection.Query<Customer>("Select * from Customers");
I would now like to return the equivalent of a DataSet dynamically from my query
SqliteDatabase.Connection.Query("Select * from Customers inner join Calls on Customers.Id=Calls.CustomerId")
Now from the second query I would like to return a DataSet instead of a list of objects. I know I could create a new object which combines the columns of Customers and Calls but I don't want to have to create objects every time I want to query the database.
Is it possible to just dynamically return a Dataset or Object?
In the end I actually managed to come up with a method that will run any query and return the rows as items in the list and the columns as objects in the array:
public List<object[]> RunSql(string sqlString, bool includeColumnNamesAsFirstRow)
{
var lstRes = new List<object[]>();
SQLitePCL.sqlite3_stmt stQuery = null;
try
{
stQuery = SQLite3.Prepare2(fieldStrikeDatabase.Connection.Handle, sqlString);
var colLenght = SQLite3.ColumnCount(stQuery);
if (includeColumnNamesAsFirstRow)
{
var obj = new object[colLenght];
lstRes.Add(obj);
for (int i = 0; i < colLenght; i++)
{
obj[i] = SQLite3.ColumnName(stQuery, i);
}
}
while (SQLite3.Step(stQuery) == SQLite3.Result.Row)
{
var obj = new object[colLenght];
lstRes.Add(obj);
for (int i = 0; i < colLenght; i++)
{
var columnType = SQLitePCL.raw.sqlite3_column_decltype(stQuery, i);
switch (columnType)
{
case "text":
obj[i] = SQLite3.ColumnString(stQuery, i);
break;
case "int":
obj[i] = SQLite3.ColumnInt(stQuery, i);
break;
case "real":
obj[i] = SQLite3.ColumnDouble(stQuery, i);
break;
case "blob":
obj[i] = SQLite3.ColumnBlob(stQuery, i);
break;
case "null":
obj[i] = null;
break;
}
}
}
return lstRes;
}
catch (Exception)
{
return null;
}
finally
{
if (stQuery != null)
{
SQLite3.Finalize(stQuery);
}
}
}
SQLite.NET PCL is a .NET wrapper around sqlite.
Therefore you can query similar to EF by using a join in in LINQ or Lambda than in the Query. The wrapper will handle the conversion to sqlite query for you.
You can then return a new datatype of the joined type or a dynamic type.
Note : Joins are not directly supported in sqlite (more info) and work around is mentioned here.
Sample code:
var conn = new SQLiteConnection(sqlitePlatform, "foofoo");
var query = from customer in conn.Table<Customers>().ToList()
join call in conn.Table<Calls>().ToList()
on customer.ID equals call.CustomerId
select new { Customer = customer , Calls = call };
Lambda version:
conn.Table<Customer>().ToList().Join
(conn.Table<Call>().ToList(),
customer => customer.Id,
call => call.CustomerId,
(customer, call) => new { Customer = customer, Calls = call });
thank u so much user1! works perfect.
here is just an example how to use ur method:
var objects = mySQLiteConnection.RunSql("SELECT * FROM Persons", true);
// ColumnNames
List<string> ColumnNames = new List<string>();
for (int column = 0; column < objects[0].Length; column++)
{
if (objects[0][column] != null) spaltennamen.Add((string)objects[0][column]);
}
// RowValues
for (int row = 1; row < objects.Count; row++)
{
for (int column = 0; column < objects[row].Length; column++)
{
if (objects[row][column] != null) System.Diagnostics.Debug.WriteLine(spaltennamen[column] + " : " + objects[row][column]);
}
}
It sounds like what you want to do is essentially recreate ADO.NET. When you say "DataSet", I'm guessing that you are talking about ADO.NET. This probably means that you don't want to use the ORM functionality built in to the SQLite.Net library.
I have created this version of the library that will allow you to do flat table reads from an SQLite database. It means that you CAN read the data in to an ADO.NET dataset if you like.
https://github.com/MelbourneDeveloper/SQLite.Net.Standard
Unlike #Fabian Monkemoller, i was unable to get #User1's code to work straight away. This is a modified version that make use of nullable reference types and method-nesting to seperate the main-code from the try-catch block:
public static object?[][]? ToDataSet(this SQLiteConnection sqlConnection, string query , bool includeColumnNamesAsFirstRow = true)
{
var stQuery = SQLite3.Prepare2(sqlConnection.Handle, query );
var colLength = SQLite3.ColumnCount(stQuery);
try
{
return SelectRows().ToArray();
}
catch (Exception e)
{
return null;
}
finally
{
if (stQuery != null)
{
SQLite3.Finalize(stQuery);
}
}
IEnumerable<object?[]> SelectRows()
{
if (includeColumnNamesAsFirstRow)
{
yield return SelectColumnNames(stQuery, colLength).ToArray();
}
while (SQLite3.Step(stQuery) == SQLite3.Result.Row)
{
yield return SelectColumns(stQuery, colLength).ToArray();
}
static IEnumerable<object> SelectColumnNames(SQLitePCL.sqlite3_stmt stQuery, int colLength)
{
for (int i = 0; i < colLength; i++)
{
yield return SQLite3.ColumnName(stQuery, i);
}
}
static IEnumerable<object?> SelectColumns(SQLitePCL.sqlite3_stmt stQuery, int colLength)
{
for (int i = 0; i < colLength; i++)
{
var x = SQLitePCL.raw.sqlite3_column_decltype(stQuery, i);
yield return x switch
{
"text" => SQLite3.ColumnString(stQuery, i),
"integer" => SQLite3.ColumnInt(stQuery, i),
"bigint" => SQLite3.ColumnInt64(stQuery, i),
"real" => SQLite3.ColumnDouble(stQuery, i),
"blob" => SQLite3.ColumnBlob(stQuery, i),
"null" => null,
_ => throw new Exception($"Unexpected type encountered in for query {stQuery}")
};
}
}
}
}
My program should renumber footnotes in a word file.
We have a VBA-Macro, that does the same but it´s too slow.
Dim fussnote As Footnote
For Each fussnote In ActiveDocument.Footnotes
fussnote.Reference.Select
With Selection
With .FootnoteOptions
.Location = wdBottomOfPage
.NumberingRule = wdRestartContinuous
.StartingNumber = 1
.NumberStyle = wdNoteNumberStyleArabic
.NumberingRule = wdRestartSection
End With
.Footnotes.Add range:=Selection.range, Reference:=""
End With
Next
I tried to clone all footnotes, then go through them (and their references) and change their ID (is Id the right property?)
My Code looks like this (not working):
public override void Work(WordprocessingDocument args)
{
var __allFootnotes = (Footnotes)args.MainDocumentPart
.FootnotesPart.Footnotes.Clone();
var footnotes = __allFootnotes.Elements<Footnote>()
.SkipWhile(f => !(f.Id.Value > 0)).ToList();
RenumberFootnotes(footnotes,
args.MainDocumentPart.Document.Body.Descendants<Paragraph>().ToList());
var __styles = args.MainDocumentPart
.StyleDefinitionsPart.Styles;
for (int i = 0; i < footnotes.Count(); i++)
{
//var footnote = footnotes[i];
}
args.MainDocumentPart.FootnotesPart
.Footnotes = __allFootnotes;
}
private void RenumberFootnotes(List<Footnote> footnotes, List<Paragraph> paragraphs)
{
var __p = paragraphs.Where(p => p.Descendants<FootnoteReference>().Any());
var __references = __p.SelectMany(p => p.Descendants<FootnoteReference>());
for (int i = 1; i < footnotes.Count; i++)
{
var __tempId = footnotes[i].Id.Value;
footnotes[i].Id.Value = i;
var __reference = __references.First(fr => fr.Id.Value == __tempId);
__reference.Id.Value = i;
}
}
Solution extracted from question:
You have to add a new 'FootnoteReferenceMark' Object into a Run in your Footnote.
'footnote' is my variable for a footnote. Then i just take the first descendant of type 'Run' and append a new child of 'FootnoteReferenceMark'.
footnote.Descendants<Run>().First().AppendChild(new FootnoteReferenceMark());
I'm pulling data from Dynamics AX 2009 from c# using the following code snippet. This works fine, except for those cases where the underlying field type is a dimension. I want to be able to "flatten" array types when I return them but can't see any way to do this. Any ideas anyone?
axRecord = ax.CreateAxaptaRecord(tableName);
axRecord.ExecuteStmt(strQuery);
// Loop through the set of retrieved records.
using (StreamWriter sw = File.CreateText(path))
{
AxaptaObject axDictTable = ax.CreateAxaptaObject("SysDictTable",axRecord.get_Field("tableid"));
outputRow = null;
List<int> ids = new List<int>();
for (int i = 1; i <= (int)axDictTable.Call("fieldCnt"); i++)
{
AxaptaObject axDictField = ax.CreateAxaptaObject("DictField", axRecord.get_Field("tableid"), axDictTable.Call("fieldCnt2ID", i));
outputRow += ((string)axDictField.Call("Name")) + ",";
ids.Add((int)axDictTable.Call("fieldCnt2ID", i));
}
sw.WriteLine(outputRow);
while (axRecord.Found)
{
outputRow = null;
foreach(int i in ids)
outputRow += axRecord.get_Field(i).ToString().Replace(",", "") + ",";
sw.WriteLine(outputRow);
axRecord.Next();
}
}
You could check if the field is an array by (X++ code), and then work this out your way to "flatten" it:
currentTable = new SysDictTable(tablenum(ledgerJournalTable));
for(i = 0;i<=currentTable.fieldCntWithoutSys();i++)
{
currentField = new SysDictField(currentTable.id(), currentTable.fieldCnt2Id(i));
if(currentField.arraySize() > 1)
{
//your code
}
}
You can always safely cast the object like this:
var o = ax.CreateAxaptaObject() as AxaptaObject;
if(o != null)
{
...code
}