I am creating a DataTable dynamically, NOT from SQL. I need to count the number of times the value of '0' appears in the 'testresult' column. So far everything I've tried returns a count of 0, even though the table has 1 entry of '0' in the testresult column. HELP! :)
I've tried:
1)
DataRow[] rows = dtMerged.Select("testresult Like '%0%'");
int rowcount = rows.Length;
int rowcount = dtMerged.Select("testresult = 0").Count();
int rowcount = Convert.ToInt32(dtMerged.Compute("Count(testresult)", "testresult Like '%0%'"));
var rowcount = dtMerged.AsEnumerable()
.GroupBy(r => r.Field<string>("testresult"))
.Select(r => new
{
Str = r.Key,
Count = r.Count()
});
All of these options give me a rowcount of '0' except number 4, which resulted in a value of 'null'. Here is an image of the DataTable in question.
Thanks!
Here is a sample of code that will create essentially the same DataTable that I'm working with, my creation method is a bit complicated for copy/pasting:
DataTable dtMerged= new DataTable();
dtMerged.Columns.Add("ID", typeof(int));
dtMerged.Columns.Add("hbserial", typeof(string));
dtMerged.Columns.Add("testresult", typeof(string));
dtMerged.Rows.Add(0, "testhashboard1", "1");
dtMerged.Rows.Add(1, "samplehashboard1", "1");
dtMerged.Rows.Add(2, "samplehashboard2", "0");
return dtMerged;
//Attempt 1
DataRow[] rows = dtMerged.Select("testresult Like '%0%'");
int rowcount = rows.Length;
//Attempt 2
int rowcount = dtMerged.Select("testresult = 0").Count();
//Attempt 3
int rowcount = Convert.ToInt32(dtMerged.Compute("Count(testresult)", "testresult Like '%0%'"));
//Attempt 4
var rowcount = dtMerged.AsEnumerable()
.GroupBy(r => r.Field<string>("testresult"))
.Select(r => new
{
Str = r.Key,
Count = r.Count()
});
Related
I have Data Table with the following data
Number Type Order count
1 1 R 1
1 1 R 1
1 1 R 1
1 2 R 1
I am looking to get to this result
Number Type Order count
1 1 R 3
1 2 R 1
How can I group by three columns
var result = dt.AsEnumerable()
.GroupBy(x => {x.Field<string>("Number"))//need to group by Type and order also need to sum te total counts
rgoal
Your question made me curious, so I did some digging on Stack Overflow.
esc's answer appears will also solve your issue. It is posted under: How do I use SELECT GROUP BY in DataTable.Select(Expression)?:
Applying his method to your problem gave me this solution:
DataTable dt2 = dt.AsEnumerable()
.GroupBy(r => new { Number = r["Number"], Type = r["Type"], Order = r["Order"] })
.Select(g =>
{
var row = dt.NewRow();
row["Number"] = g.Key.Number;
row["Type"] = g.Key.Type;
row["Order"] = g.Key.Order;
row["Count"] = g.Count();
return row;
}).CopyToDataTable();
This will return a DataTable matching the schema of the input DataTable with the grouping and counts you requested.
Here is the full code I use to verify in LINQPad:
DataTable dt = new DataTable("Demo");
dt.Columns.AddRange
(
new DataColumn[]
{
new DataColumn ( "Number", typeof ( int ) ),
new DataColumn ( "Type", typeof ( int ) ),
new DataColumn ( "Order", typeof ( string ) ),
new DataColumn ( "Count", typeof ( int ) )
}
);
dt.Rows.Add(new object[] { 1,1,"R", 1 });
dt.Rows.Add(new object[] { 1,1,"R", 1 });
dt.Rows.Add(new object[] { 1,1,"R", 1 });
dt.Rows.Add(new object[] { 1,2,"R", 1 });
DataTable dt2 = dt.AsEnumerable()
.GroupBy(r => new { Number = r["Number"], Type = r["Type"], Order = r["Order"] })
.Select(g =>
{
var row = dt.NewRow();
row["Number"] = g.Key.Number;
row["Type"] = g.Key.Type;
row["Order"] = g.Key.Order;
row["Count"] = g.Count();
return row;
}).CopyToDataTable();
foreach (DataRow row in dt2.Rows)
{
for (int i = 0; i < dt2.Columns.Count; i++)
Console.Write("{0}{1}",
row[i], // Print column data
(i < dt2.Columns.Count - 1)? " " : Environment.NewLine); // Print column or row separator
}
Here are the results:
1 1 R 3
1 2 R 1
I have a datatable that looks like this
The output I'm trying to achieve is described in the picture, where I want to group by the month of the datetime, and agent I suppose. This is what I have so far.
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("Agent", typeof(string));
dtTemp.Columns.Add("Amount", typeof(decimal));
dtTemp.Columns.Add("Date", typeof(DateTime));
dtTemp = dtTemp.AsEnumerable()
.GroupBy(r => r[0])
.Select(g =>
{
var row = dt.NewRow();
row[0] = g.Key;
row[1] = g.Sum(r => (decimal)r[1]);
return row;
})
.CopyToDataTable();
Any ideas how to achieve this?
Thanks in advance!
From analyzing your post, you need to group by data table rows by column Agent and only month and year from column Date.
You need to take one temporary data table dt that can hold data type for each column in your resultant data table result.
So CopyToDataTable() will copy all of your dtTemp group data to new result data table with temporary dt columns data type.
DataTable dt = new DataTable();
dt.Columns.Add("Agent", typeof(string));
dt.Columns.Add("Amount", typeof(decimal));
dt.Columns.Add("Date", typeof(string));
DataTable result = dtTemp.AsEnumerable()
.Select(x => new
{
Agent = x.Field<string>("Agent"),
Amount = x.Field<decimal>("Amount"),
Date = x.Field<DateTime>("Date").ToString("MM-yyyy")
})
.GroupBy(x => new { x.Agent, x.Date })
.Select(g =>
{
var r = dt.NewRow();
r["Agent"] = g.Key.Agent;
r["Amount"] = g.Sum(c => c.Amount);
r["Date"] = g.FirstOrDefault().Date;
return r;
})
.CopyToDataTable();
Output:
var temp = dtTemp.AsEnumerable().GroupBy(grp => new { grpmonth = Convert.ToDateTime(grp["Date"]).Month, grpyear = Convert.ToDateTime(grp["Date"]).Year, grpagent = grp["Agent"] })
.Select(val =>
{
var row = dtTemp.NewRow();
row["Agent"] = val.FirstOrDefault()["Agent"];
row["Amount"] = val.Sum(amt => Convert.ToDecimal(amt["Amount"]));
row["Date"] = val.FirstOrDefault()["Date"];
return row;
}
)
.CopyToDataTable();
For reference
So from your input sequence, you want all used Agents, with the total sum of the Amounts per month.
Let's assume your DataTable is a sequence of Rows, and that it easily can be converted to a sequence of Rows:
class RowData
{
public string Agent {get; set}
public DateTime Date {get; set;}
public int Amount {get; set;}
}
IEnumerable<RowData> tableData = ...
The solution if your problem is to make groups of RowData with equal value for Agent, and to group these groups again to make subgroups with equal value for year and month
var AgentsWithAmountsPerMonth = tableData
.GroupBy(row => row.Agent, // make groups of rows with same Agent
// ResultSelector: get the Agent (=key), with all rows that have this Agent
(agent, rowsWithThisAgent) => new
{
Agent = agent,
// to calculate the totals per year/month, extract the year / month / amount
TotalsPerMonth = rowsWithThisAgent.Select(row => new
{
Year = row.Date.Year,
Month = row.Date.Month,
Amount = row.Amount,
})
// and group by same Year / Month:
.GroupBy(row => new {row.Year, row.Month},
// ResultSelector
(yearMonth, rowsWithThisYearMonth) => new
{
Year = yearMonth.Year,
Month = yearMonth.Month,
Total = rowsWithThisYearMont.Select(row => row.Amount).Sum(),
// Or put the year and month in one field:
Month = new DateTime(yearMonth.Year, yearMonth.Month, 1),
},
});
});
What I m trying to do is relatively simple. I would like to use linq to compute some aggregated function on a group and then put the result back into a datatable of the same format. I did a lot of research and think I should use System.Data.DataSetExtensions and copy to datatable funtion. Here is my random datatable:
DataTable ADataTable = new DataTable("ADataTable");
// Fake table data
ADataTable.Columns.Add("PLANT", typeof(int));
ADataTable.Columns.Add("PDCATYPE_NAME", typeof(int));
ADataTable.Columns.Add("Month", typeof(int));
ADataTable.Columns.Add("Year", typeof(int));
ADataTable.Columns.Add("STATUS_NAME_REPORT", typeof(string));
ADataTable.Columns.Add("SAVINGS_PER_MONTH", typeof(double));
for (int i = 0; i < 15; i++)
{
for (int j = 1; j < 5; j++)
{
DataRow row = ADataTable.NewRow();
row["PLANT"] = j;
row["PDCATYPE_NAME"] = j;
row["Month"] = DateTime.Now.Month;
row["Year"] = DateTime.Now.Year;
row["STATUS_NAME_REPORT"] = "Report";
row["SAVINGS_PER_MONTH"] = j*i;
ADataTable.Rows.Add(row);
}
}
Now I will clone this format and do a simple sum on it via linq:
DataTable newtable = ADataTable.Clone();
// The actual query
IEnumerable<DataRow> query = (from rows in ADataTable.AsEnumerable()
group rows by new
{
PLANT = rows.Field<int>("PLANT"),
PDCATYPE_NAME = rows.Field<int>("PDCATYPE_NAME"),
Month = rows.Field<int>("Month"),
Year = rows.Field<int>("Year"),
STATUS_NAME_REPORT = rows.Field<string>("STATUS_NAME_REPORT")
} into g
select new
{
g.Key.PLANT,
g.Key.PDCATYPE_NAME,
g.Key.Month,
g.Key.Year,
g.Key.STATUS_NAME_REPORT,
sum = g.Sum(savings => savings.Field<double>("SAVINGS_PER_MONTH")),
});
newtable = query.CopyToDataTable<DataRow>();
The LINQ works fine but as soon as I put IEnumarable DataRow in front I get error that I cannot convert anonymys type to datarow. But if I put select new datarow I get an error that fields are unknown...
How do I proceed please?
You have multiple options, First is to use reflection to create a DataTable based on IEnumerable<T> and the other options is to populate your DataTable by enumerating your query like:
var query = ADataTable.AsEnumerable()
.GroupBy(row => new
{
PLANT = row.Field<int>("PLANT"),
PDCATYPE_NAME = row.Field<int>("PDCATYPE_NAME"),
Month = row.Field<int>("Month"),
Year = row.Field<int>("Year"),
STATUS_NAME_REPORT = row.Field<string>("STATUS_NAME_REPORT")
});
foreach (var g in query)
{
newtable.LoadDataRow(new object[]
{
g.Key.PLANT,
g.Key.PDCATYPE_NAME,
g.Key.Month,
g.Key.Year,
g.Key.STATUS_NAME_REPORT,
g.Sum(savings => savings.Field<double>("SAVINGS_PER_MONTH"))
}, LoadOption.OverwriteChanges);
}
The error in your code is because of selecting an anonymous type using select new and then trying to store it in IEnumerable<DataRow>. You can't specify DataRow in select as it is not accessible directly.
You may also see: How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
This also works:
newtable2 = ADataTable.AsEnumerable().GroupBy(a => new
{
PLANT = a.Field<int>("PLANT"),
PDCATYPE_NAME = a.Field<int>("PDCATYPE_NAME"),
Month = a.Field<int>("Month"),
Year = a.Field<int>("Year"),
STATUS_NAME_REPORT = a.Field<string>("STATUS_NAME_REPORT")
}).Select(g =>
{
var row = newtable2.NewRow();
row.ItemArray = new object[]
{
g.Key.PLANT,
g.Key.PDCATYPE_NAME,
g.Key.Month,
g.Key.Year,
g.Key.STATUS_NAME_REPORT,
g.Sum(r => r.Field<double>("SAVINGS_PER_MONTH"))
};
return row;
}).CopyToDataTable();
using System.Data.DataSetExtensions (Which requires a reference)
I have two DataTables and I want to select the rows from the first one which are not present in second one
For example:
Table A
id column
1 data1
2 data2
3 data3
4 data4
Table B
id column
1 data10
3 data30
I want the result to be:
Table C
id column
2 data2
4 data4
You can use Linq, especially Enumerable.Except helps to find id's in TableA that are not in TableB:
var idsNotInB = TableA.AsEnumerable().Select(r => r.Field<int>("id"))
.Except(TableB.AsEnumerable().Select(r => r.Field<int>("id")));
DataTable TableC = (from row in TableA.AsEnumerable()
join id in idsNotInB
on row.Field<int>("id") equals id
select row).CopyToDataTable();
You can also use Where but it'll be less efficient:
DataTable TableC = TableA.AsEnumerable()
.Where(ra => !TableB.AsEnumerable()
.Any(rb => rb.Field<int>("id") == ra.Field<int>("id")))
.CopyToDataTable();
I got a solution which works without LINQ:
public DataTable CompareDataTables(DataTable first, DataTable second)
{
first.TableName = "FirstTable";
second.TableName = "SecondTable";
//Create Empty Table
DataTable table = new DataTable("Difference");
try
{
//Must use a Dataset to make use of a DataRelation object
using (DataSet ds = new DataSet())
{
//Add tables
ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });
//Get Columns for DataRelation
DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];
for (int i = 0; i < firstcolumns.Length; i++)
{
firstcolumns[i] = ds.Tables[0].Columns[i];
}
DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];
for (int i = 0; i < secondcolumns.Length; i++)
{
secondcolumns[i] = ds.Tables[1].Columns[i];
}
//Create DataRelation
DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);
ds.Relations.Add(r);
//Create columns for return table
for (int i = 0; i < first.Columns.Count; i++)
{
table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);
}
//If First Row not in Second, Add to return table.
table.BeginLoadData();
foreach (DataRow parentrow in ds.Tables[0].Rows)
{
DataRow[] childrows = parentrow.GetChildRows(r);
if (childrows == null || childrows.Length == 0)
table.LoadDataRow(parentrow.ItemArray, true);
}
table.EndLoadData();
}
}
}
For more Visit http://microsoftdotnetsolutions.blogspot.in/2012/12/compare-two-datatables.html
You can use Linq Enumerable.Except Method function to get diffence between two DataTable's Here i use firstDt and secondDt,remember both Dt's have the same structure.
var EntriesNotInB = firstDt.AsEnumerable().Select(r => r.Field<string>("abc")).Except(secondDt.AsEnumerable().Select(r => r.Field<string>("abc")));
if (EntriesNotInB.Count() > 0)
{
DataTable dt = (from row in firstDt.AsEnumerable()join id in EntriesNotInB on row.Field<string>("abc") equals id select row).CopyToDataTable();
foreach (DataRow row in dt.Rows)
{
/////Place your code to manipulate on datatable Rows
}
}
To read more on Enumerable.Except Method,Go to http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except(v=vs.110).aspx
and its Done!!!! Happy Coding.........
I am iterating on a lot of strings and I want to fill my first(and only) 3 Columns with each result and then start again in a new row. like:
A | B | C
------+--------+------
"DOG" | "CAT" | "FISH"
"FDF" | "AAA" | "RRR"
AND SO ON....
Basically after each row is "full" open new row.
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
HtmlNodeCollection rows = tables[2].SelectNodes(".//tr");
DataTable dataTable = new DataTable();
dataTable.Columns.Add("A", typeof(string));
dataTable.Columns.Add("B", typeof(string));
dataTable.Columns.Add("C", typeof(string))
try like this
for (int i = 0; i < rows .Count(); i++)
{
DataRow datarowObj= dataTable .NewRow();
datarowObj["A"] = yourValue;
datarowObj["B"] = yourValue;
datarowObj["C"] = yourValue;
dataTable.Rows.Add(datarowObj);
}
You could use Linq's GroupBy to split the long list into groups of 3:
sample-data:
DataTable table = new DataTable();
table.Columns.Add("Col1");
table.Columns.Add("Col2");
table.Columns.Add("Col3");
List<string> longList = Enumerable.Range(1, 99).Select(i => "row " + i).ToList();
group the long list into parts of three:
var groupsWithThree = longList
.Select((s, i) => new { Str = s, Index = i })
.GroupBy(x => x.Index / 3);
add them to the table:
foreach (var group3 in groupsWithThree)
table.Rows.Add(group3.First().Str, group3.ElementAt(1).Str, group3.Last().Str);
Note that it presumes that the list is divisible by three.
Manage with DataRoxw, for instance, after adding an empty DataRow to your DataTable :
DataRow row = table.Rows[0];
foreach (object item in row.ItemArray)
{
?
dataTable.Rows.Add(new object[] { "A1", "B1", "C1" })
// Alternatively
object[] arr = new object[] { "A2", "B2", "C2" };
dataTable.Rows.Add(arr);