Select all records of a datatable with duplicate values - c#

How do we select all the record of particular year (e.g. 2014) from a datatable where one of the column value(here 2014) is repeated on multiple rows of
table using linq or any other method in C#.
This is the datatable:

It's been a while since I've worked with DataTables but I think this should do it.
To select rows where the year is duplicated across more than one row:
dt.AsEnumerable().GroupBy(x => x["Year"]).Where(x => x.Count() > 1);
To select only the rows for a particular year:
dt.AsEnumerable().Where(x => x["Year"] == "2014");
Where dt is your System.Data.DataTable.

If you are trying to retrieve all the records lies on the Year 2014, use following linq query,IT will check your data column has data or not, I am sure it will works..
//selecting all the records of 2014
IEnumerable<DataRow> dtrow = default(IEnumerable<DataRow>);
dtrow = yourtable.AsEnumerable().Where(x => x.Field<Int64>("year") == Convert.ToInt64("2014"));
if (dtrow.Count() > 0)
{
dataTbl = dtrow.CopyToDataTable(); //dataTbl is the DataTable
}

Try to do it in database since it is faster than C#. DB can have index but C# does not have.
select *, count(1) as totalCount
group by year
having totalCount > 1

Related

Compare Data tables

I need to compare the two data table,
In both datatable we have the systemuserid . In datatable1 we have two rows.The system user id will start with c2dd... and 53cf...
Now i need to compare the two tables whther all systemuserids are available in second Datatable.
In these table the c2dd... sustem user is not available in the datatable 2. so i need to add that c2dd.. row in datatable 2 with noofCall as 0
If you have two datatable available, then you can compare two table and get table1 row systemuserid which are not available in table2 in following way :
IEnumerable<DataRow> differenceRows = table1.AsEnumerable()
.Where(x => table2.AsEnumerable()
.All(y => y.Field<string>("systemuserid") != x.Field<string>("systemuserid")));
After getting differenceRows, you can add new row in table2 iterating through differenceRows.

How to pivot DataTable by Column

I like to group a datatable by a known column but the rest of the columns are unknown. The first table in the picture is the source and the second table is the one i like to produce. Only the column that is needed to group by is sure to be there. I don't know the rest of the columns so it must be dynamic.
So far, i have tried using Linq but it doesn't product the output i wanted.
var dt = res.AsEnumerable()
.GroupBy(r => r.Field<string>("GroupBy"))
.SelectMany(t => t.ToList())
.CopyToDataTable();
When you talk about pivoting a table, you are usually summarizing the data in some fashion -- counting, totaling, averaging. If you only know one column, you can't really pivot it other than to count how many rows are in each group:
var dt = res
.AsEnumerable()
.GroupBy(r => r.Field<String>("ColumnToGroup"))
.Select(r => new { Key = r.Key, Count = r.Count() });
Gives you a pivot table that looks something like:
Key Count
London 2
Manchester 2
To do a useful pivot, you have to know something about the data in the table.

Merging 2 datatables in to 1 datatable with same number of rows.

How can i merge two Datatables into the same row. I am using different stored procedures to get data into datasets. In asp.net using c#, i want to merge them so there are same number of rows as table 1 with an added column from table 2.
For example:
DataTable table1 = dsnew.Tables[0];
DataTable table2 = dsSpotsLeft.Tables[0];
table1.Merge(table2);
This is fetching me 4 rows instead of 2 rows. What am i missing here? Thanks in advance!!
You cannot use the method Merge in this case, instead you should create new DataTable dt3, and then add columns and rows based on the table 1 and 2:
var dt3 = new DataTable();
var columns = dt1.Columns.Cast<DataColumn>()
.Concat(dt2.Columns.Cast<DataColumn>());
foreach (var column in columns)
{
dt3.Columns.Add(column.ColumnName, column.DataType);
}
//TODO Check if dt2 has more rows than dt1...
for (int i = 0; i < dt1.Rows.Count; i++)
{
var row = dt3.NewRow();
row.ItemArray = dt1.Rows[i].ItemArray
.Concat(dt2.Rows[i].ItemArray).ToArray();
dt3.Rows.Add(row);
}
Without knowing more about the design of these tables, some of this is speculation.
What it sounds like you want to perform is a JOIN. For example, if you have one table that looks like:
StateId, StateName
and another table that looks like
EmployeeId, EmployeeName, StateId
and you want to end up with a result set that looks like
EmployeeId, EmployeeName, StateId, StateName
You would perform the following query:
SELECT Employee.EmployeeId, Employee.EmployeeName, Employee.StateId, State.StateName
FROM Employee
INNER JOIN State ON Employee.StateId = State.StateId
This gives you a resultset but doesn't update any data. Again, speculating on your dataset, I'm assuming that your version of the Employee table might look like the resultset:
EmployeeId, EmployeeName, StateId, StateName
but with StateName in need of being populated. In this case, you could write the query:
UPDATE Employee
SET Employee.StateName = State.StateName
FROM Employee
INNER JOIN State ON Employee.StateId = State.StateId
Tested in SQL Server.
Assuming you have table Category and Product related by CategoryID, then try this
var joined = from p in prod.AsEnumerable()
join c in categ.AsEnumerable()
on p["categid"] equals c["categid"]
select new
{
ProductName = p["prodname"],
Category = c["name"]
};
var myjoined = joined.ToList();
Sources
LINQ query on a DataTable
Inner join of DataTables in C#
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/ecb6a83d-b9b0-4e64-8107-1ca8757fe58c/
That was a LINQ solution. You can also loop through the first datatable and add columns from the second datatable

C# Linq filter DataTable using array elements

I want filter the data in a data table using linq.
My scenario is I have an array of elements which contains dates created dynamically and in the data table we have columns as id,date,etc.
We have to retrieve the id's which contains all the dates in array
ex:
string[] arr={"10/10/2012","11/11/2012","9/9/2012"}
Table :
ID date
1 10/10/2012
2 11/11/2012
1 9/9/2012
6 9/9/2012
3 9/9/2012
6 11/11/2012
1 11/11/2012
Output would be 1 - because only id '1' has all the array elements.
To accomplish above functionality I am using the Linq query shown below. But I am literally failing.
Dim volunteers As DataTable =
(From leftTable In dtavailableVolunteers.AsEnumerable()
Join rightTable In dtavailableVolunteers.AsEnumerable()
On leftTable.VolunteerId Equals rightTable.VolunteerId
Where SelectedDatesArray.All(Function(i) rightTable.Field(Of String)("SelectedDate").Equals(i.ToString()))
Select rightTable).CopyToDataTable()
Lets say your datatable is dt
DataRow[] dr = dt.Select("date in (" + string.join("," , arr) + ")");
string[] st = dr.Select(ss => ss["id"].ToString()).ToArray();
OR
DataTable newdt = dr.CopyToDataTable();
Second line is of LINQ
You could group the rows by ID, and then find the groups where: there does not exist an arr element which the group's dates doesn't contain that element. I mean something like:
var result = from item in list
group item by item.ID into grouping
where !arr.Exists(date =>
!grouping.Select(x => x.Date).Contains(date))
select grouping.Key;
Here is another version:
from volunteer in dtavailableVolunteers
group volunteer by volunteer.Id into g
let volunteerDates = g.Select(groupedElement=>groupedElement.date)
where arr.All(date=>volunteerDates.Contains(date))
select g.Key

linq select distinct date from a DataTable

I want to fetch distinct date from a DataTable. Currently I'm using the below code:
MyDataTable.DefaultView.ToTable(true, "MyDateColumn");
This code considers the time too but I don't want the time to be considered. I wrote the below code to select only Date Part but while making the distinct it considers the time too.
MyDataTable.AsEnumerable()
.Select(row => new { MyDateColumn = row.Field<DateTime>("MyDateColumn").ToString("d") }).Distinct();
Please help me to select only distinct date(i.e by ignoring time).
You can try Selecting the column by just the Date property of the column:
MyDataTable.AsEnumerable()
.Select(row => row.Field<DateTime>("MyDateColumn").Date).Distinct();

Categories

Resources