I’m a LINQ beginner and need some help. Below is the important part of the generated XML generated by a rest query.
<FormData FormOID="F_TABLEOFMODAL_V20" OpenClinica:Version="v2.0" OpenClinica:Status="initial data entry">
<ItemGroupData ItemGroupOID="IG_TABLE_MODALITYTABLE" ItemGroupRepeatKey="3" TransactionType="Insert">
<ItemData ItemOID="I_TABLE_MODAL_DATE_TABLE" Value="2014-04-10" />
<ItemData ItemOID="I_TABLE_MODAL_TYPE_TABLE" Value="4" />
</ItemGroupData>
<ItemGroupData ItemGroupOID="IG_TABLE_MODALITYTABLE" ItemGroupRepeatKey="1" TransactionType="Insert">
<ItemData ItemOID="I_TABLE_MODAL_DATE_TABLE" Value="2014-04-01" />
<ItemData ItemOID="I_TABLE_MODAL_TYPE_TABLE" Value="2" />
</ItemGroupData>
<ItemGroupData ItemGroupOID="IG_TABLE_MODALITYTABLE" ItemGroupRepeatKey="2" TransactionType="Insert">
<ItemData ItemOID="I_TABLE_MODAL_DATE_TABLE" Value="2014-04-04" />
<ItemData ItemOID="I_TABLE_MODAL_TYPE_TABLE" Value="1" />
</ItemGroupData>
</FormData>
What i want to get to are the ItemOIDs. My question is is there some way to get Values for all ItemOID="I_TABLE_MODAL_DATE_TABLE"(ans: two dates) and ItemOID="I_TABLE_MODAL_TYPE_TABLE" (ans: two numbers). The code below works but this uses ItemData for the first element (called date) and then skipping one to second element (type).
////Print out xml to use?
var doc = XDocument.Load(XmlReader.Create(streaming));
XNamespace nsSys = "http://www.cdisc.org/ns/odm/v1.3";
//loop through <ItemGroupData> to get both date and type value
//from each iteration
var items = from i in doc.Descendants(nsSys + "ItemGroupData")
select new
{
date = (string)i.Element(nsSys + "ItemData").Attribute("Value"),
type = (string)i.Descendants(nsSys + "ItemData").Skip(1).First().Attribute("Value")
};
//Catch here?
DataTable modDt = new DataTable();
modDt.Columns.Add(new DataColumn("Date", typeof(string)));
modDt.Columns.Add(new DataColumn("Type", typeof(string))); //changed to string?
//add LINQ-to-XML query result to DataTable
foreach (var item in items)
{
//convert item.date string to DateTime
//then convert it back to string with different format
modDt.Rows.Add(DateTime.ParseExact(item.date, "yyyy-MM-dd", CultureInfo.InvariantCulture)
.ToString("MM/dd/yy"),
int.Parse(item.type));
}
//Value of OC output
//1=mr,2=ct,3=pet,4=us,5=meg,6=ecg (put if statements before xnat rest ssh string)
for (int i = 0; i <= modDt.Rows.Count - 1; i++)
{
modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("1", "mr");
modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("2", "ct");
modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("3", "pet");
modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("4", "us");
modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("5", "meg");
modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("6", "ecg");
}
I've started writting some code shown below. The adding of data into datatable still does not work sorry. However the idea of what i'm trying to do is in there.
DataTable modDt = new DataTable();
modDt.Columns.Add(new DataColumn("Date", typeof(string)));
modDt.Columns.Add(new DataColumn("Type", typeof(string))); //changed to string?
var document = XDocument.Load("doc.xml");
XNamespace nsSys = "http://www.cdisc.org/ns/odm/v1.3";
//Get nodes separated by inner child element
var itd = document.Descendants(nsSys + "ItemData")
.Where(t => (string)t.Attribute("ItemOID") == "I_TABLE_MODAL_DATE_TABLE");
var itds = itd.Attributes("Value").ToArray();
foreach (var it in itds)
{
var subA = it.ToString();
var subAA = subA.ToCharArray();
var subB = subAA.Skip(9).Take(8).ToArray();
string sub = new string(subB);
var subBB = sub.Split('-').ToArray();
string subC = subBB[1] + "/" + subBB[2] + "/" + subBB[0];
foreach (DataRow dr in modDt.Rows)
{
dr["Date"] = subC;
}
Console.WriteLine(subC);
}
var itt = document.Descendants(nsSys + "ItemData")
.Where(t => (string)t.Attribute("ItemOID") == "I_TABLE_MODAL_TYPE_TABLE");
var itty = itt.Attributes("Value").ToArray();
foreach (var et in itty)
{
var subz = et.ToString();
var subzz = subz.ToCharArray();
var subx = subzz.Skip(7).Take(1).ToArray();
string subxx = new string(subx);
foreach (DataRow dt in modDt.Rows)
{
dt["Type"] = subxx;
}
Console.WriteLine(subxx);
}
//for (int i = 0; i <= modDt.Rows.Count - 1; i++)
//{
// modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("1", "mr");
// modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("2", "ct");
// modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("3", "pet");
// modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("4", "us");
// modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("5", "meg");
// modDt.Rows[i][1] = modDt.Rows[i][1].ToString().Replace("6", "ecg");
//}
foreach (DataRow row in modDt.Rows) // Loop over the rows.
{
string sessionDate = row["Date"].ToString();
string mod = row["Type"].ToString();
string drow = "aa" + sessionDate + "bb" + mod + "cc";
Console.WriteLine(drow);
}
Console.Read();
Thank you so much for your help and your time. It is greatly appreciated. Cheers.
Current Attempt
DataTable modDt = new DataTable();
modDt.Columns.Add(new DataColumn("Date", typeof(string)));
modDt.Columns.Add(new DataColumn("Type", typeof(string))); //changed to string?
var document = XDocument.Load("doc.xml");
XNamespace nsSys = "http://www.cdisc.org/ns/odm/v1.3";
//Get nodes separated by inner child element
var values = document.Descendants(nsSys + "ItemData")
.Where(t => t.Attribute("ItemOID").Value == "I_TABLE_MODAL_DATE_TABLE")
.Select(x => x.Attribute("Value").Value);
foreach (var item in values)
{
modDt.Rows.Add(DateTime.ParseExact(item, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("MM/dd/yy"),null);
}
var typevalues = document.Descendants(nsSys + "ItemData")
.Where(t => t.Attribute("ItemOID").Value == "I_TABLE_MODAL_TYPE_TABLE")
.Select(x => x.Attribute("Value").Value);
foreach (var item in typevalues)
{
modDt.Rows.Add(null ,int.Parse(item));
}
foreach (DataRow row in modDt.Rows)
{
string sessionDate = row["Date"].ToString();
string mod = row["Type"].ToString();
string xnatCli = mod + sessionDate;
Console.Write(xnatCli);
}
i am trying to answer your question in a single line of code
var values = document.Descendants("ItemData")
.Where(t => t.Attribute("ItemOID").Value == "I_TABLE_MODAL_DATE_TABLE")
.Select(x => x.Attribute("Value").Value)
after this line the values collection will have all the dates. same can be applied to other queries as well.
Update: the solution below use linq to sql, assuming class FormDataContext is prepared for connecting database to relevant table.
FormDataContext db = new FormDataContext();
[Table(Name = "FormData")]
public class FormData
{
[Column]
public DateTime Date;
[Column]
public int Type;
}
IEnumerable<FormData> values = document.Descendants("ItemGroupData").Select(t => {
return new FormData {
FormDate = Convert.ToDateTime(t.XPathSelectElement("ItemData[#ItemOID='I_TABLE_MODAL_DATE_TABLE']").Attribute("Value").Value),
Type = Convert.ToInt32(t.XPathSelectElement("ItemData[#ItemOID='I_TABLE_MODAL_TYPE_TABLE']").Attribute("Value").Value)
};
});
foreach (FormData data in values) {
db.Form.InsertOnSubmit(data);
}
db.SubmitChanges();
This is just an example to store the required two fields to db. You may adjust the example based on your needs.
Related
I have a project that I am working on, which is related to the work of daily shifts for employees, so that I have made the code for filling in the dates and their days in the relevant table ResultTB as follows and it's work correctly
var dt1 = DateTime.Parse(txtFromDate.Text);
var dt2 = DateTime.Parse(txtToDate.Text);
var dt = DateTime.Parse(txtFromDate.Text);
if (dt <= dt2)
{
dt = dt.AddDays(-1);
while (dt2 >= dt1)
{
List<ResultTB> ResultList = new List<ResultTB>
{
new ResultTB{NameOfDay = dt1.DayOfWeek.ToString(),DateOfDay = dt=dt.AddDays(1) }
};
foreach (var item in ResultList)
{
dt1 = dt1.AddDays(1);
db.ResultTBs.InsertOnSubmit(item);
}
db.SubmitChanges();
DGVResult.DataSource = db.ResultTBs.ToList();
}
}
Now I would like to make an update to the table ResultTB to columns EmpName+EmpID from EmployeeTB, so that the code searches in the table EmployeeTB which have the status of the employee “possible” and assigns it to a date in order, and this is the code that I wrote, but it adds the first person in the EmployeeTB table and assigns it to all the dates in the table ResultTB.
var EmpList = db.EmployeeTBs
.Where(x => x.EmpStatus == "possible").ToList();
foreach (var EmpItem in EmpList)
{
var ListOfResult = db.ResultTBs.Where(r => r.EmpName == null).ToList();
foreach (var itemResult in ListOfResult)
{
itemResult.EmpID = EmpItem.EmpID;
itemResult.EmpName = EmpItem.EmpName;
}
db.SubmitChanges();
}
I tried many solutions but it didn't work with me please help me to solve this problem. Thanks
var EmpList = db.EmployeeTBs.Where(x => x.EmpStatus == "possible").ToList();
for (int i=0; i< EmpList.length; i++)
{
var ListOfResult = db.ResultTBs.Where(r => r.EmpName == null).ToList();
ListOfResult[i].EmpID = EmpList[i].EmpID
ListOfResult[i].EmpName = EmpList[i].EmpName
}
db.SubmitChanges();
If I understood your question correctly this should work.
this question has been asked but mine has a different approach link 1
l have a datatable with the following data
DataTable dtProduct = new DataTable();
dtProduct.Columns.Add("productId");
dtProduct.Columns.Add("name");
DataRow dataRow = dtProduct.NewRow();
dataRow["productId"] = 1;
dataRow["name"] = "Burger";
dtProduct.Rows.Add(dataRow);
DataRow dataRow2 = dtProduct.NewRow();
dataRow2["productId"] = 2;
dataRow2["name"] = "Chicken";
dtProduct.Rows.Add(dataRow2);
DataTable dtSales = new DataTable();
dtSales.Columns.Add("productId");
dtSales.Columns.Add("saleId");
dtSales.Columns.Add("month");
dtSales.Columns.Add("quantity");
DataRow dataSalesRow = dtSales.NewRow();
dataSalesRow["productId"] = 1;
dataSalesRow["saleId"] = "1";
dataSalesRow["month"] = "Jan";
dataSalesRow["quantity"] = 3433;
dtSales.Rows.Add(dataSalesRow);
DataRow drSales2 = dtSales.NewRow();
drSales2["productId"] = 1;
drSales2["saleId"] = "2";
drSales2["month"] = "Feb";
drSales2["quantity"] = 56;
dtSales.Rows.Add(drSales2);
DataRow drSales3 = dtSales.NewRow();
drSales3["productId"] = 1;
drSales3["saleId"] = "3";
drSales3["month"] = "Mar";
drSales3["quantity"] = 34522;
dtSales.Rows.Add(drSales3);
DataRow drSales4 = dtSales.NewRow();
drSales4["productId"] = 2;
drSales4["saleId"] = "4";
drSales4["month"] = "Feb";
drSales4["quantity"] = 345;
dtSales.Rows.Add(drSales4);
And another sample 2
DataTable dtStudents = new DataTable();
dtStudents.Columns.Add("studentId");
dtStudents.Columns.Add("fullname");
DataRow drStudentrow = dtStudents.NewRow();
drStudentrow["studentId"] = 1;
drStudentrow["fullname"] = "Bil";
dtStudents.Rows.Add(drStudentrow);
DataRow drStudentrow2 = dtStudents.NewRow();
drStudentrow2["studentId"] = 2;
drStudentrow2["fullname"] = "Paul";
dtStudents.Rows.Add(drStudentrow2);
DataTable dtStudentExam = new DataTable();
dtStudentExam.Columns.Add("studentId");
dtStudentExam.Columns.Add("subjectId");
dtStudentExam.Columns.Add("mark");
DataRow dataStudentExamRow = dtStudentExam.NewRow();
dataStudentExamRow["studentId"] = 1;
dataStudentExamRow["subjectId"] = "E123";
dataStudentExamRow["mark"] = 34;
dtStudentExam.Rows.Add(dataStudentExamRow);
DataRow dataStudentExamRow2 = dtStudentExam.NewRow();
dataStudentExamRow2["studentId"] = 2;
dataStudentExamRow2["subjectId"] = "E123";
dataStudentExamRow2["mark"] = 90;
dtStudentExam.Rows.Add(dataStudentExamRow2);
DataRow dataStudentExamRow3 = dtStudentExam.NewRow();
dataStudentExamRow3["studentId"] = 1;
dataStudentExamRow3["subjectId"] = "E155";
dataStudentExamRow3["mark"] = 78;
dtStudentExam.Rows.Add(dataStudentExamRow3);
DataRow dataStudentExamRow4 = dtStudentExam.NewRow();
dataStudentExamRow4["studentId"] = 1;
dataStudentExamRow4["subjectId"] = "E101";
dataStudentExamRow4["mark"] = 12;
dtStudentExam.Rows.Add(dataStudentExamRow4);
DataRow dataStudentExamRow5 = dtStudentExam.NewRow();
dataStudentExamRow5["studentId"] = 1;
dataStudentExamRow5["subjectId"] = "E234";
dataStudentExamRow5["mark"] = 42;
dtStudentExam.Rows.Add(dataStudentExamRow5);
DataTable products(dtProduct ) has a productId which is the key and another data table sales (dtSales) has sales for the product based on month . and has a column productId .
what l want to achieve is merge this two tables to this
Same Applies for students
l have a data table student (dtStudents) but l however l have student exam marks data tables (dtStudentExam) for each subject
and combine to something like this
So my question is can l do this dynamically . l tried the following below
public static System.Data.DataTable MergeRowsToColumns(DataTable rowDataTable, string rowDataTableIdColumnName, string friendlyName, DataTable columData)
{
List<string> columnsToAdd = new List<string>();
if (rowDataTable == null)
{
return null;
}
DataTable finalDataTable = new DataTable();
finalDataTable.Columns.Add(friendlyName.ToLower());
finalDataTable.Columns.Add(rowDataTableIdColumnName.ToLower());
foreach (DataColumn column in columData.Columns)
{
if (column.ColumnName.ToString() == rowDataTableIdColumnName.ToString())
{
continue;
}
else
{
finalDataTable.Columns.Add(column.ColumnName.ToString());
columnsToAdd.Add(column.ColumnName.ToString());
}
}
foreach (DataRow row in rowDataTable.Rows)
{
DataRow newRow = finalDataTable.NewRow();
newRow[rowDataTableIdColumnName] = row[rowDataTableIdColumnName];
newRow[friendlyName] = row[friendlyName];
foreach (DataRow columnRows in columData.Rows)
{
foreach (string column in columnsToAdd)
{
var value = columnRows[column].ToString();
newRow[column] = value;
}
}
finalDataTable.Rows.Add(newRow);
}
return finalDataTable;
}
Algorithm logic
Table 1 holds the key information
Table 2 holds the value data for table
using a column name from table one for id which will be the same on table 2
can l do it generically
in one function like
var dtMergedProducts = HelperDataTable.MergeRowsToColumns(dtProduct, "productId", "name", dtSales);
var dtStudents = HelperDataTable.MergeRowsToColumns(dtStudents, "studentId", "fullname", dtStudentExam);
Please am retrieving the data as is hence can not group by in table 2 and also table two does not have a friendly name that will be displayed.
Most of the data that is loaded is not more than 100 000
this is the link of the helper utility am making git hub
Edit One
DataTable 2 will have column value one for column and the other for value
For example
dtSales
1- month : shown vertically
2- quantity: will be cell value
Sample 2
dtStudentExam
1- subjectId : shown vertically
2- mark : will be the student mark
l got it working this morning
here if the method
public static System.Data.DataTable MergeRowsToColumns(DataTable rowDataTable, string rowDataTableIdColumnName, string friendlyName, DataTable dataAllValue , string dataColumnValue , string dataColumnKey)
{
List<string> columnsToAdd = new List<string>();
if (rowDataTable == null)
{
return null;
}
DataTable finalDataTable = new DataTable();
finalDataTable.Columns.Add(friendlyName.ToLower());
finalDataTable.Columns.Add(rowDataTableIdColumnName.ToLower());
foreach (DataRow row in dataAllValue.Rows)
{
if (row[rowDataTableIdColumnName].ToString() == rowDataTableIdColumnName.ToString())
{
continue;
}
else
{
var isExistColumnValue = columnsToAdd.Where(c => c == row[dataColumnKey].ToString()).FirstOrDefault();
if(isExistColumnValue == null)
{
columnsToAdd.Add(row[dataColumnKey].ToString());
finalDataTable.Columns.Add(row[dataColumnKey].ToString());
}
}
}
foreach (DataRow row in rowDataTable.Rows)
{
DataRow newRow = finalDataTable.NewRow();
newRow[rowDataTableIdColumnName] = row[rowDataTableIdColumnName];
newRow[friendlyName] = row[friendlyName];
foreach (DataRow columnRows in dataAllValue.Rows)
{
if(row[rowDataTableIdColumnName].ToString() != columnRows[rowDataTableIdColumnName].ToString())
{
continue;
}
var columnName = columnRows[dataColumnKey].ToString();
var columnValue = columnRows[dataColumnValue].ToString();
newRow[columnName] = columnValue;
}
finalDataTable.Rows.Add(newRow);
}
return finalDataTable;
}
it looked like l was failing on adding the column names before merging
you can just call like
var dt = HelperDataTable.MergeRowsToColumns(dtProduct, "productId", "name", dtSales , "quantity", "month");
var dt2 = HelperDataTable.MergeRowsToColumns(dtStudents, "studentId", "fullname", dtStudentExam , "mark", "subjectId");
I have two list (1st with values from a website, 2nd with values from a .csv file) and I'd like to join them in another list, starting two equals values, and display it in a datagridview.
Before to post the code, I'd like to say that I tried to fill my datagridview with these two lists separately and they work.
I didn't get any error, but I can't see my datagridview with values.
I'm going to post my code and explain it.
First List Code:
var url = textBox5.Text;
//var url = "http://www.betexplorer.com/soccer/norway/tippeligaen/results/";
var web = new HtmlWeb();
var doc = web.Load(url);
Bets = new List<Bet>();
// Lettura delle righe
var Rows = doc.DocumentNode.SelectNodes("//tr");
foreach (var row in Rows)
{
if (!row.GetAttributeValue("class", "").Contains("rtitle"))
{
if (string.IsNullOrEmpty(row.InnerText))
continue;
var rowBet = new Bet();
foreach (var node in row.ChildNodes)
{
var data_odd = node.GetAttributeValue("data-odd", "");
if (string.IsNullOrEmpty(data_odd))
{
if (node.GetAttributeValue("class", "").Contains("first-cell"))
{
rowBet.Match = node.InnerText.Trim();
var matchTeam = rowBet.Match.Split(new[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
rowBet.Home = matchTeam[0];
rowBet.Host = matchTeam[1];
}
if (node.GetAttributeValue("class", "").Contains("result"))
{
rowBet.Result = node.InnerText.Trim();
var matchPoints = rowBet.Result.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
int help;
if (int.TryParse(matchPoints[0], out help))
{
rowBet.HomePoints = help;
}
if (matchPoints.Length == 2 && int.TryParse(matchPoints[1], out help))
{
rowBet.HostPoints = help;
}
}
if (node.GetAttributeValue("class", "").Contains("last-cell"))
rowBet.Date = node.InnerText.Trim();
}
else
{
rowBet.Odds.Add(data_odd);
}
}
if (!string.IsNullOrEmpty(rowBet.Match))
Bets.Add(rowBet);
}
}
Second List & Combined List Code:
string FileName = #"C:\mydir\testcsv.csv";
OleDbConnection conn = new OleDbConnection
("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " +
Path.GetDirectoryName(FileName) +
"; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter
("SELECT * FROM " + Path.GetFileName(FileName), conn);
DataSet ds = new DataSet("Temp");
adapter.Fill(ds);
conn.Close();
// DataTable dt = new DataTable();
DataTable dt = ds.Tables[0];
//dataGridView2.DataSource = dt;
// dataGridView2.DataMember = "Table";
List<HT> matchlist = new List<HT>();
matchlist = (from DataRow dr in dt.Rows
select new HT()
{
Home = dr["Home"].ToString().Replace("Milan", "AC Milan").Replace("Roma", "AS Roma"),
Host = dr["Host"].ToString().Replace("Milan", "AC Milan").Replace("Roma", "AS Roma"),
ScoreHome = dr["ScoreHome"].ToString(),
ScoreAway = dr["ScoreAway"].ToString(),
//Segno = dr["Segno"].ToString(),
//odd1 = dr["odd1"].ToString(),
//oddx = dr["oddx"].ToString(),
//odd2 = dr["odd2"].ToString()
}).ToList();
// dataGridView2.DataSource = matchlist;
var combinedDataList = (from d1 in Bets
//join d2 in dataList2 on d1.Home equals d2.Home
join d2 in matchlist on new { d1.Home, d1.Host } equals new { d2.Home, d2.Host }
select new CombinedData
{
Data = d1.Date,
Home = d1.Home,
Away = d1.Host,
HSFT = d1.HomePoints,
ASFT = d1.HostPoints,
HSHT = d2.ScoreHome,
ASHT = d2.ScoreAway,
HODD = d1.odd1,
XODD = d1.oddX,
AODD = d1.odd2,
RisFin = d1.RisFin,
Over05SH = d1.over05sh,
Over05FT = d1.Over05FT,
Over15FT = d1.Over15FT,
Over25FT = d1.Over25FT,
Over35FT = d1.Over35FT,
Over45FT = d1.Over45FT
}).OrderBy(p => p.HODD);
dataGridView2.DataSource = combinedDataList;
Thank you for your attention. Have a fantastic sunday!
EDIT: I delete unnecessary code
EDIT2: I add the screen of my single list output. Let's see:
First List:
Second List:
So, I'd like to merge "ScoreHome" and "ScoreAway" from the second list in my first list based on "Home" and "Host" that I have in both lists.
This is a take on some experimental code that #Tim Schmelter pointed me in the correct direction towards earlier this afternoon. The majority of it is almost exactly the same as what worked earler, but it is throwing a invalidCastException on the last line or second last line, depending whichever i try. I cannot see why this is.
Boolean test = false;
string filePathStudent = System.IO.Path.GetFullPath("StudentInfo.txt");
DataTable studentDataTable = new DataTable();
studentDataTable.Columns.Add("Id", typeof(int));
studentDataTable.Columns.Add("StudentID");
studentDataTable.Columns.Add("FirstName");
studentDataTable.Columns.Add("LastName");
studentDataTable.Columns.Add("StreetAdd");
studentDataTable.Columns.Add("City");
studentDataTable.Columns.Add("State");
studentDataTable.Columns.Add("Zip");
studentDataTable.Columns.Add("Choice1");
studentDataTable.Columns.Add("CreditHrs1");
studentDataTable.Columns.Add("Choice2");
studentDataTable.Columns.Add("CreditHrs2");
studentDataTable.Columns.Add("Choice3");
studentDataTable.Columns.Add("CreditHrs3");
studentDataTable.Columns.Add("Choice4");
studentDataTable.Columns.Add("CreditHrs4");
studentDataTable.Columns.Add("Choice5");
studentDataTable.Columns.Add("CreditHrs5");
studentDataTable.Columns.Add("Choice6");
studentDataTable.Columns.Add("CreditHrs6");
foreach (string line in File.ReadLines(filePathStudent))
{
DataRow row = studentDataTable.Rows.Add();
string[] fields = line.Split(new[] { (char)9 });
int id;
if (fields.Length == 19 && int.TryParse(fields[0], out id))
{
row.SetField("Id", id);
row.SetField("StudentID", fields[1]);
row.SetField("FirstName", fields[2]);
row.SetField("LastName", fields[3]);
row.SetField("StreetAdd", fields[4]);
row.SetField("City", fields[5]);
row.SetField("State", fields[6]);
row.SetField("Zip", fields[7]);
row.SetField("Choice1", fields[8]);
row.SetField("CreditHrs1", fields[9]);
row.SetField("Choice2", fields[10]);
row.SetField("CreditHrs2", fields[11]);
row.SetField("Choice3", fields[12]);
row.SetField("CreditHrs3", fields[13]);
row.SetField("Choice4", fields[14]);
row.SetField("CreditHrs4", fields[15]);
row.SetField("Choice5", fields[16]);
row.SetField("CreditHrs5", fields[17]);
row.SetField("Choice6", fields[18]);
row.SetField("CreditHrs6", fields[19]);
}
}
using (StreamReader reader = new StreamReader(filePathStudent))
{
String line1 = reader.ReadLine();
if (line1 == null)
maxIDStdTable = 0;
else
test = true;
reader.Dispose();
reader.Close();
}
if(test)
int maxIDStdTable = studentDataTable.AsEnumerable().Max(r => r.Field<int>("Id"));
//int maxIDStdTable = (int)studentDataTable.Compute("Max(Id)", "");
You have made two mistakes :
1) You have create new DataRow with DataTable.NewRow()
2) Sfter setting DataRow you have to add it to the DataTable with DataTable.Rows.Add(youDataRow).
Update your code as and try:
foreach (string line in File.ReadLines(filePathStudent))
{
DataRow row = studentDataTable.NewRow();
string[] fields = line.Split(new[] { (char)9 });
int id;
if (fields.Length == 19 && int.TryParse(fields[0], out id))
{
row["Id"]= id;
row["StudentID"]= fields[1];
row["FirstName"]= fields[2];
row[LastName"]= fields[3];
row["StreetAdd"]= fields[4];
row["City"]=fields[5];
row["State"]= fields[6];
row["Zip"]=fields[7];
row["Choice1"]= fields[8];
row["CreditHrs1"]= fields[9];
row["Choice2"]= fields[10];
row["CreditHrs2"]= fields[11];
row[("Choice3"]= fields[12];
row["CreditHrs3"]=, fields[13];
row["Choice4"]= fields[14];
row["CreditHrs4"]= fields[15];
row["Choice5"]= fields[16];
row["CreditHrs5"]= fields[17];
row["Choice6"]= fields[18];
row["CreditHrs6"] =fields[19];
}
studentDataTable.Rows.Add(row);
}
It might not be the best solution, but it works.
string filePathStudent = System.IO.Path.GetFullPath("StudentInfo.txt");
DataTable studentDataTable = new DataTable();
studentDataTable.Columns.Add("Id", typeof(Int32));
studentDataTable.Columns.Add("StudentID");
studentDataTable.Columns.Add("FirstName");
studentDataTable.Columns.Add("LastName");
studentDataTable.Columns.Add("StreetAdd");
studentDataTable.Columns.Add("City");
studentDataTable.Columns.Add("State");
studentDataTable.Columns.Add("Zip");
studentDataTable.Columns.Add("Choice1");
studentDataTable.Columns.Add("CreditHrs1");
studentDataTable.Columns.Add("Choice2");
studentDataTable.Columns.Add("CreditHrs2");
studentDataTable.Columns.Add("Choice3");
studentDataTable.Columns.Add("CreditHrs3");
studentDataTable.Columns.Add("Choice4");
studentDataTable.Columns.Add("CreditHrs4");
studentDataTable.Columns.Add("Choice5");
studentDataTable.Columns.Add("CreditHrs5");
studentDataTable.Columns.Add("Choice6");
studentDataTable.Columns.Add("CreditHrs6");
// Read in a file line-by-line, and store it
var txtFileLine = File.ReadAllLines(filePathStudent).ToList();
//Reads line splits data to colums at tab (ASCII value 9)
txtFileLine.ForEach(line => studentDataTable.Rows.Add(line.Split((char)9)));
List<int> rowsForColumn1 = studentDataTable.AsEnumerable().Select(x => x.Field<int>(0)).ToList();
//Tests for empty Datatable
foreach (DataRow row in studentDataTable.Rows)
{
if (row.IsNull("Id"))
break;
else
//get max value from "Id" row.
maxIDStdTable = rowsForColumn1.Max();
}
}
is it possible to convert data table to ienumerable without know its class name.
my requirement is to convert table
First | Last
--------------
john | mcgill
clara | linda
to
{{First:john,Last:mcgill},{First:clara ,Last:linda}}
Ienumerable collection
i dont want to use dynamic object because dynamic object supports only frame work 4.
thanks
var results = from row in dataTable.AsEnumerable()
select new {
First = row.Field<string>("First"),
Last = row.Field<string>("Second")
};
You'll need System.Data.DataSetExtensions.
You can use Anonymous Types - they were introduced with .NET 3.5.
Syntax for that kind of objects is really clear and intuitive:
var item = new { First = "First-Value", Last = "Last-Value" }
and the query:
var items = dataTable.AsEnumerable()
.Select(i => new {
First = i.Field<string>("First"),
Last= i.Field<string>("Last")
});
No column names please!
public string ConvertDataTableToString(DataTable table)
{
int iColumnCount = table.Columns.Count;
int iRowCount = table.Rows.Count;
int iTempRowCount = 0;
string strColumName = table.Columns[0].ColumnName;
string strOut = "{";
foreach (DataRow row in table.Rows)
{
strOut = strOut + "{";
foreach (DataColumn col in table.Columns)
{
string val = row.Field<string>(col.ColumnName);
strOut = strOut + col.ColumnName + ":" + val;
if (col.Ordinal != iColumnCount - 1)
{
strOut = strOut + ",";
}
}
strOut = strOut + "}";
iTempRowCount++;
if (iTempRowCount != iRowCount)
{
strOut = strOut + ",";
}
}
strOut = strOut + "}";
return strOut;
}
It is a fairly easy job using anonymous types. Here is a complete example that only requires classes from the System.Linq and System.Data namespaces:
class Program
{
static void Main(string[] args)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add().ColumnName = "First";
dataTable.Columns.Add().ColumnName = "Last";
var row = dataTable.NewRow();
row["First"] = "hello";
row["Last"] = "world";
dataTable.Rows.Add(row);
var query = dataTable.Rows.Cast<DataRow>()
.Select(r => new
{
First = r["First"],
Last = r["Last"]
});
foreach (var item in query)
Console.WriteLine("{0} {1}", item.First, item.Last);
}
}