calculating the occurrence of two column values in DataTable using linq - c#

I have a data table, which contains set of Attributes like (First Name, Last Name, Gender etc..)
I wanna count the occurrence of value in only two columns or Attribute.
For example, I have 4 columns in my datatable (FirstName, LastName, Age, Gender).
I want to know if any two or more records in the datatable shares the same first name and last name.
I have no idea where to start, any idea can save my day
thank you

Yes, you can use LINQ(-To-DataTable).
Group by an anonympous type containing both properties:
var duplicates = table.AsEnumerable()
.GroupBy(r => new {
FirstName = r.Field<string>("First Name"),
LastName = r.Field<string>("Last Name")
})
.Where(g => g.Count() > 1)
.Select(g => new { Person = g.Key, Count = g.Count(), Rows = g.ToList() });

var duplicates = dt.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1);
dt is datatable.

Related

Filtering datatable where sum of grouped column value is not zero

Can we display the data table rows where grouped column value is not zero.
first, I need to group by sno and display ungrouped data where amount is not zero.
For example:
I have a data table like below:
need output like this
However I get this with the below code
FinalDataTable = table.AsEnumerable()
.GroupBy(r => new { Col1 = r["sno"]})
.Select(g =>{
var row = table.NewRow();
row["sno"] = g.Key.Col1;
row["amount"] = g.Sum(r => r.Field<decimal>("amount"));
return row;
}).CopyToDataTable();
Yes, what you want to do is Group by sno and then use Where to find those records where the Sum of amount is not zero. Then simply use Select Many to unwrap those groups back into rows.
I think this should do it (I've assumed amount is an int)
FinalDataTable = table.AsEnumerable()
.GroupBy(r => r["sno"])
.Where(g => g.Sum(r => (int)r["amount"]) != 0)
.SelectMany(r => r)
.CopyToDataTable();
Here is a live example: https://dotnetfiddle.net/ixi0aW

How to remove same values in LinqToSql But get same rows count

I have about 20000 rows data in Table in db.
I want to get 3 rows data in this table , but I don't want to get same rows.
Example code:
var SampleTable = (from v in db.Article
select new Item
{
Name = v.Name
});
var distinctItems = SampleTable.GroupBy(x => x.Name ).Select(y => y.First());
var threeItem = distinctItems.Take(3).ToList();
If I use var distinctItems = SampleTable.GroupBy(x => x.Name ).Select(y => y.First()); and get 3 rows.
It work, but large data rows will take a large time to do GroupBy.
But if I take 3 rows in SampleTable first, then remove same rows.
If remove rows happen that will cause my rows count not
meet the requirements count.
Have a better way to do?

How to achieve Count(Distinct {x}) from dataTable using c#?

I want to find out the count of the distinct values of a particular column of a DataTable and store the result in listbox.
Currently, I have the code to extract the distinct values of a column of a DataTable and store it in a ListBox as follows:-
var ids = dt.AsEnumerable()
.Select(s => new
{
EventID = s.Field<Int32>("ID"),
})
.Distinct().ToList();
listBox.DataSource = ids;
where dt is the name of the DataTable
ID is the name of the column
For Eg:-
Table is
Name ID Marks
Ashish 10 200
Ram 100 300
Sur 200 800
Shim 10 899
Kam 100 989
Then the result in the listbox should appear as:-
{ID=10}{Count=2}
{ID=100}{Count=2}
{ID=200}{Count=1}
Currently I am getting output as:-
{ID=10}
{ID=100}
{ID=200}
Please help!!
You can use GroupBy:
var idCounts = dt.AsEnumerable()
.GroupBy(row => row.Field<int>("ID"))
.Select(g => new
{
EventID = g.Key,
Count = g.Count()
})
.ToList();
listBox.DataSource = idCounts;

LINQ query on Datatable - Pulling multiple columns error

I have a datatable which holds 3 columns:
Product, Price, Manufacturer
I am trying to read the data into a list of objects, defining which rows are stored by the following:
Store all Products which have the cheapest price and take the
manufacturer from that line.
EG-
Product, Price, Manufacturer
table, 15.00, ikea
table, 12.50, woodpty
chair, 11.00, ikea
chair, 9.00, woodpty
The expected output into the list is two objects with the following properties:
table, 12.50, woodpty
chair, 9.00, woodpty
I have the following code, but I am getting an error-
String does not contain a definition for 'Name' and no extension
method 'Name' accepting a first argument of type 'string' could be
found (are you missing an assembly reference?)
var result = (
from row in dtProductListings.AsEnumerable()
group row by row.Field<string>("Product") into g
let x = new
{
Name = g.Key.Name, //THIS LINE IS CAUSING THE PROBLEM
Price = g.Min(x => x.Field<float>("Price"))
}
where (row.Name == x.Name && row.Price == x.Price)
select new Foo
{
Name = row.Name,
Manufacturer = row.Manufacturer,
Price = row.Price
}
).ToList();
I am still quite new to LINQ and am wondering where I am going wrong? Any help would be greatly appreciated.
Nice try but your attempt has a few issues. Firstly where you have used Name = g.Key.Name should be Name = g.Key, and secondly your Linq expression will fail to compile also because row is no longer in scope after the group by clause.
Linq can be a tricky thing to get your head around at the start, but what you're effectively trying to achieve is to group the rows by the product column and then select the row in each group which has the lowest price.
So to create the product groups:
var rowsGroups = from row in dtProductListings.AsEnumerable()
group row by row.Field<string>("Product") into g
select g.OrderBy(row => row.Price);
For your example, this will produce two groups of IOrderedEnumerable based on the product values, with the items in each group being order by lowest price to highest price.
Group 1: Key = "table"
Row 1: table, 12.50, woodpty
Row 2: table, 15.00, ikea
Group 2: Key = "chair"
Row 1: chair, 9.00, woodpty
Row 2: chair, 11.00, ikea
So now all your have to do to get your result is to select the first item in each group to get the minimum priced item:
var result = (from row in rowGroups
select row.First())
.ToList();
The same query using lambda expression and linq chain methods (which I find easier to write since it focuses you on the inputs and outputs of what your're doing):
var result = dtProductListings.AsEnumerable()
.GroupBy(row => row.Field<string>("Product"))
.Select(x => x.OrderBy(y => y.Price))
.Select(x => x.First())
.ToList();
Simplified further:
var result = dtProductListings.AsEnumerable()
.GroupBy(row => row.Field<string>("Product"))
.Select(x => x.OrderBy(y => y.Price).First())
.ToList();
Try this:
var result = (
from row in dtProductListings.AsEnumerable()
group row by row.Field<string>("Product") into g
select new
{
Name = g.Key,
Price = g.Min(x => x.Field<float>("Price"))
Manufacturer = g.First().Field<string>("Manufacturer")
}
).ToList();

Count & Search the Data on DataTable

I have four columns name SrNo,RollNo,Name,Age in my datatable and corresponding values as
SrNo ,Roll No,Name,Age
1, 1, ABC, 20
2, 2, DEF, 22
3, 3, ABC, 25
I want search how many different a names are present & their count.
Please suggest
Thanks
The simplest way to do this would probably be with LINQ (IMO, anyway):
var groups = table.AsEnumerable()
.GroupBy(x => x.Field<string>("Name"))
.Select(g => new { Name = g.Key, Count = g.Count() });
That's assuming you really do have the data in a DataTable. If it's actually still in the database, you can use a similar LINQ to SQL query:
var groups = dataContext.GroupBy(x => x.Name)
.Select(g => new { Name = g.Key, Count = g.Count() });
Actually you could use an overload of GroupBy to do it all in one method call:
var groups = dataContext.GroupBy(x => x.Name,
(key, group) => new { Name = key,
Count = group.Count() });
select count(1) as cnt, Name from mytable group by Name
Write a SQL query that creates this summary and execute it using ADO.NET.
If you want to use sql server. Below is the answer
Select Name, count(Name)
From YourTableNamew
Group by Name
SELECT COUNT(DISTINCT column_name) FROM table_name group by column_name

Categories