This should be a simple one, but I'm a beginner with C#.
Given a glossary list in the following format:
aptitude
ability, skill, gift, talent
aqueous
watery
arguably
maybe, perhaps, possibly, could be
How can I parse this, and insert into a database table in the format:
TABLE: Term_Glossary
================================================
Term_Name | Term_Definition |
================================================
aptitude | ability, skill, gift, talent |
------------------------------------------------
aqueous | watery |
------------------------------------------------
arguably | maybe, perhaps, possibly, could be|
================================================
Any help would be appreciated - thanks.
Update
I realize the database structure is simple/inefficient - but really, the point of my question is the code to parse the kind of text found in the first example, using C#. Thanks.
It may seem more complex at first, but you'll find it a lot easier in the long-term to think in terms of two tables:
===========================================
Term_ID | Term_Name |
===========================================
1 | aptitude |
2 | aqueous |
3 | arguably |
===========================================
===============================================
Definition_ID | Term_ID | Definition_Name |
===============================================
1 | 1 | ability |
2 | 1 | skill |
3 | 1 | gift |
4 | 1 | talent |
5 | 2 | watery |
6 | 3 | maybe |
7 | etc.etc.etc
Perhaps even think if you can normalise this further by having one table of words with IDs and a table of associations.
It looks to me like you would read the first line, save it to a variable, read the second line, save it to a second variable, then insert into the table where Term_Name = first variable, and Term_Definition = second variable.
So your logic would be like:
StreamReader SR;
string Term_Name;
string Term_Definition
SR = File.OpenText(filename);
Term_Name = SR.ReadLine();
while(Term_Name != null)
{
Term_Definition = SR.ReadLine();
// make your database call here to insert with these two variables. I don't know what DB you are using.
Term_Name = SR.ReadLine();
}
SR.Close();
Related
I have this ef query that give me the following result
IQueryable<A>
| Id | count |
| 1 | 5 |
| 2 | 6 |
IQueryable<B>
| Id | count |
| 1 | 1 |
| 2 | 2 |
| 3 | 9 |
When I do
IQueryable<Something> C = A.union(B)
Result that I got is this
| Id | count |
| 1 | 5 |
| 2 | 6 |
| 1 | 1 |
| 2 | 2 |
| 3 | 9 |
Whish is logical.
What I want is a UnionBy(Id)
IQueryable<Something> C = A.unionBy(B,c=>c.Id)
and this work perfectly in my case
| Id | count |
| 1 | 5 | -- FROM A
| 2 | 6 | -- FROM A
| 3 | 9 | -- FROM B
If the Query A or B are already executed by that I mean a ToList() was made it work perfectly and I have no problem in anyway.
But in my case, both queries are not executed and thus using this function result in.
System.InvalidOperationException query could not be translated.
the alternative is to use a GroupBy however I have no idea how to replacte UnionBy behavior with the GroupBy
FYI: the query works perfectly using the IQueryable.Union
and it's mandatory in my case that the request stay in IQueryable and not executed until later
UPDATE
⚠️ The solution that I'm looking for must stay in IQueryable without a toList() execution
"query could not be translated" usually means that EF doesn't support a certain LINQ or language construct as it can't translate it into SQL. One way to make this work is to force the evaluation of the expression client-side by adding e.g. ToList() or likes on the query parts before executing the UnionBy:
IQueryable<Something> C = A.ToList().UnionBy(B.ToList(),c=>c.Id);
The solution is simple you filtre A From B using the following
IQueryable<Something> C = A.Union(B.where(b=> A.All(a=>a.Id != b.Id))
I'm attempting to populate a C# TreeView from the output of the DOS Tree command(tree /F /A > treeList.txt). I need to determine the level of each node in the text file line by line and store it as an integer. Is there a way that this can be determined through Regex expressions? Below is an example of the output from the Tree command:
Folder PATH listing
Volume serial number is ****-****
C:.
| info.txt
| treeList.txt
|
+---Folder1
| +---Sub1
| | | info.txt
| | | info2.txt
| | | info3.txt
| | |
| | \---Sub
| | | info.txt
| | |
| | \---Sub
| | info.txt
| | info2.txt
| |
| +---Sub2
| \---Sub3
+---Folder2
| | info.txt
| | info2.txt
| |
| +---Sub1
| | info.txt
| |
| +---Sub2
| +---Sub3
| | info.txt
| |
| \---Sub4
+---Folder3
| \---Sub1
+---Folder4
| +---Sub1
| \---Sub2
| info.txt
| info2.txt
|
\---Folder5
info.txt
This is an example of the output I'm trying to achieve:
info.txt 0
treeList.txt 0
Folder1 0
Sub1 1
info.txt 2
info2.txt 2
info3.txt 2
Sub 2
info.txt 3
Sub 3
info.txt 4
info2.txt 4
Folder2 0
And so on...
Any assistance or guidance is greatly appreciated.
I have an idea. You may want to replace in every line of the tree's string every special character of the tree by replacing:
[|\\-\+]
and after that count spaces between beginning of the line and name of the file or folder.
Number of spaces will tell You how deep you are in lvl.
Then You also may divide number of spaces by 3 and You will get aproximately a number of lvl.
What do You think?
Expression used to split text at beginning of node:
((?:[a-zA-Z0-9][+'-_ ()a-zA-Z0-9.]*))
Code used to determine Node level:
List<TreeItem> items=new List<TreeItem>();
int lineNum=0;
string line;
// Read the file
StreamReader file=new StreamReader("<Path>");
while((line=file.ReadLine())!=null) {
string[] parts=Regex.Split(line,"((?:[a-zA-Z0-9][+'-_ ()a-zA-Z0-9.]*))");
if(parts.Length>1) {
//Node level is determined by the number of characters preceding node text
items.Add(new TreeItem(parts[1],(parts[0].Length/4)-1));
}
lineNum++;
}
file.Close();
I have a FIX log file. I'm iterating on the lines, putting each string into
Message m = new Message(str, false)
Because for some reason, validation fails on the file (even the first line). Now, I see that it's a 35=X type, and 268=4 (i.e. NoMDEntries=4, so I should have 4 groups in the message)
BUT, in the debug display I am not seeing any groups. m.base._groups has a count of 0.
The string in question is:
1128=9 | 9=363 | 35=X | 49=CME | 34=3151 | 52=20121216223556363 | 75=20121217 | 268=4 | 279=0 | 22=8 | 48=43585 | 83=902 | 107=6EH3 | 269=4 | 270=13186 | 273=223556000 | 286=5 | 279=0 | 22=8 | 48=43585 | 83=903 | 107=6EH3 | 269=E | 270=13186 | 271=9 | 273=223556000 | 279=0 | 22=8 | 48=43585 | 83=904 | 107=6EH3 | 269=F | 270=13185 | 273=223556000 | 279=1 | 22=8 | 48=43585 | 83=905 | 107=6EH3 | 269=0 | 270=13186 | 271=122 | 273=223556000 | 336=0 | 346=10 | 1023=1 | 10=179 |
Another thing is how do I read the groups? Instinctively, I want to do something like
for (int i = 1; i <= noMDEntries; i++) {
Group g = m.GetGroup(i);
int action = Int32.Parse(g.GetField(279));
....
}
But that's not how it works and I haven't found documentation with better explanations.
Thanks for the help,
Yonatan.
From your code snippets, I think you're using QuickFIX/n, the native C# implementation, so I will answer accordingly.
1) Your message construction is failing because you didn't provide a DataDictionary.
Use Message::FromString instead:
Message m = new Message();
m.FromString(msg_str, false, data_dic, data_dic, someMsgFactory);
Even better, use MarketDataIncrementalRefresh::FromString to get the right return type.
You can see some uses of this function here:
https://github.com/connamara/quickfixn/blob/master/UnitTests/MessageTests.cs
2) To read groups... well, QF/n has a doc page on that, which I think explains it pretty well.
http://quickfixn.org/tutorial/repeating-groups
I am developing windows project using c#. I have to import text file records into sqlserver 2005 db.
My question is
I want to read records from text file using student Idno.
I have an idea of read exact value from text file using strings that is finding student no:1001 value .
I want to read every student record based on particular student no? not for 1001
so I want standered code to read particular records based on student number as
my choice.
plz help me.......
my text file like this
|-----------------------------------------------|
| student no:1001 |
| address:ongole,A.P, |
| hyderabad,india. |
| ----------------------------------------------|
| stdid stdname class |
| 1 raheem mca |
| 2 sudheer mca |
| ----------------------------------------------|
| student no:1001 |
| address:ongole,A.P, |
| hyderabad,india. |
-|-------------------------------------------- |
| stdid stdname class |
| 1 raheem mca |
| 2 sudheer mca |
| ----------------------------------------------|
| student no:1002 |
| address:ongole,A.P, |
| hyderabad,india. |
------------------------------------------------|-
| student no:1003 |
| address:ongole,A.P, |
| hyderabad,india. |
-|---------------------------------------------- |
My code is
private void BtnImpstrm_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
string f = openFileDialog1.FileName;
StreamReader sr = new StreamReader(f);
string s = sr.ReadToEnd();
MessageBox.Show(s);
sr.Close();
str =s.Split('\n');
for (int i = 0; i < str.Length; i++)
{
if (str[i].ToString().Trim().ToUpper().Contains("Student NO : 1001"))
{
string[] words = str[i].Split(' ');
string output = words[word no].ToString().Trim();
string rec1 =str[line no].ToString().Trim();
string rec2 = str[line no].ToString().Trim();
MessageBox.Show(output);
MessageBox.Show(rec1);
MessageBox.Show(rec2);
}
}
}
I think your best bet would be to use StreamReader to read line-by-line and if you encounter "student ..." or something like it, read the next 7 lines to complete your record. After doing this, you will have a collection of strings, each being a unique record in the file.
Then you can parse each record individually, by splitting the string on Environment.NewLine.
A little background information: I have a table called table_a, which has 12 columns. I want to insert or update rows with values for 6 of these columns, while I don't wanna lose the data in the other 6 columns. And I wanna do this with a parameterized query in C#.
field1 is Unique.
> SELECT * FROM table_a;
+----+--------+--------+---+---------+---------+
| Id | field1*| field2 |...|field11 | field12 |
+----+--------+--------+---+---------+---------+
| 1 | AA | BB |...| KK | LL |
| 2 | AA | BB |...| KK | LL |
| 3 | AA | BB |...| KK | LL |
| 4 | AA | BB |...| KK | LL |
+----+--------+--------+---+---------+---------+
The Problem is, my first thought was to use REPLACE INTO, unfortunately this will delete the 6 not touched values:
> REPLACE INTO table_a (field1, ..., field6) VALUES ('AA', ...);
> REPLACE INTO table_a (field1, ..., field6) VALUES ('AB', ...);
+----+--------+--------+---+---------+---------+
| Id | field1*| field2 |...| field11 | field12 |
+----+--------+--------+---+---------+---------+
| 1 | AA | BB |...| NULL | NULL |
| 2 | AB | BB |...| NULL | NULL |
| 3 | AC | BB |...| KK | LL |
| 4 | AD | BB |...| KK | LL |
+----+--------+--------+---+---------+---------+
My second thought was to use INSERT INTO ... ON DUPLICATE KEY UPDATE, but then I'd have to bind the parameters a second time, the first time in the INSERT part and the second time in the UPDATE part, like this:
INSERT INTO table_a (field1, ..., field6)
VALUES(?, ..., ?)
ON DUPLICATE KEY UPDATE
field1 = ?, ..., field6 = ?;
That would preserve my data, but I have to bind the parameters twice.
The third option would be to create another two queries and use the SELECT and INSERT INTO/UPDATE pattern.
So, my question is, how do I do this the smart way?
Your second option sounds like a winner for single row updates.
Your third option is good if you insert/update many rows at once (as it will not matter much that you have two queries then - providing each does only what it is supposed to do).
UPDATE:
Digging through documentation one finds that you can bind once if you wish - you can refer to the originally bound values with VALUES()
UPDATE2:
Well, actually you can not get to the bound values with VALUES(column), so instead two suggestions that actually might help:
did you check about using named parameters (then you would not need to bound them twice)?
did you consider stored procedures?
I think you've listed all the available options, along with the pros/cons of each. As for the third option, you would probably want to wrap your two queries in a transaction to ensure that the operation remains atomic.
Hi let's say you want to modify field2 to field6.
why wouldn't you do:
replace into table_a select field1,new_value2,...,new_value6,field7,...,field12 from table_a where field1=filter_field1;
You put the new values and you get the others value by querying the table you're updating.