Count & Search the Data on DataTable - c#

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

Related

calculating the occurrence of two column values in DataTable using linq

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.

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();

Linq Query Count and GroupBy

Can anyone tell me how to do this SQL Query in Linq?
SELECT [id], COUNT(*) FROM [data].[dbo].[MyTable] GROUP BY [id]
You can try this approach:
var res = ctx.MyTable // Start with your table
.GroupBy(r => r.id) / Group by the key of your choice
.Select( g => new {Id = g.Key, Count = g.Count()}) // Create an anonymous type w/results
.ToList(); // Convert the results to List
you can try this
var res = from r in MyTable
group p by r.id into grouped
select new {id = g.key, total = g.Count()};
then, when you have to use it, just do ToList()
Also you can do it after the select new.
I don't have Visual Studio 2010 here to try it, but
I think it will work

LINQ: Group by aggregate but still get information from the most recent row?

Let's say I have a table that holds shipping history. I'd like to write a query that counts the amount of shipments per user and gets the shipping name from the most recent entry in the table for that user.
Table structure for simplicity:
ShipmentID
MemberID
ShippingName
ShippingDate
How do I write a LINQ C# query to do this?
It sounds like might want something like:
var query = from shipment in context.ShippingHistory
group shipment by shipment.MemberID into g
select new { Count = g.Count(),
MemberID = g.Key,
MostRecentName = g.OrderByDescending(x => x.ShipmentDate)
.First()
.ShipmentName };
Not really a LINQ answer, but personally, I'd be dropping to SQL for that, to make sure it isn't doing any N+1 etc; for example:
select s1.MemberID, COUNT(1) as [Count],
(select top 1 ShippingName from Shipping s2 where s2.MemberID = s1.MemberID
order by s2.ShippingDate desc) as [LastShippingName]
from Shipping s1
group by s1.MemberID
You can probably do LINQ something like (untested):
var qry = from row in data
group row by row.MemberId into grp
select new {
MemberId = grp.Key,
Count = grp.Count(),
LastShippingName =
grp.OrderByDescending(x => x.ShippingDate).First().ShippingName
};

How do i get the top 5 items in a database based on the occurence of a particluar field?

Using Ado.Net Entity framework, I am trying to get the 'top 3' items in a table based on the amount of times they appear in a table.
For example:
Table:
basket_to_product_id | basket_id | product_id
I want to see how many times product_id occurs, and would like to return the top 3 product_ids that occur the most frequently.
I'm stuck at:
List<BasketToProduct> btplist = entities.BasketToProduct. ..........?
Something like this should work (of course I do not know the actual names of your properties):
IEnumerable<int> top3ProductIds = (from btp in entities.BasketToProduct
group btp by btp.ProductId into g
orderby g.Count() descending
select g.Key).Take(3);
You could try to use a LINQ query on the table.
Try this:
var query = entities.BasketToProduct
.GroupBy(btp => btp.ProductID)
.Select(g => ProductID = g.Key, Count = g.Count())
.OrderBy(g => g.Count)
.Take(3);
It'll get you the top three ProductIDs and their associated counts.

Categories