Solved

Linq Query Question: Chart Dataset

Posted on 2012-03-28
2
307 Views
Last Modified: 2012-03-29
Let's say we have this table...

Table: PARTS
ID     PartName      DateTimeCreated
1       Hammer         7/1/2012 01:22:33
2       Shovel             7/1/2012 01:22:33      
3       Nail                 7/2/2012 01:22:33
4       Wrench           7/3/2012 01:22:33
5       Screw              7/3/2012 01:22:33
6       Pliers              7/3/2012 01:22:33

and we need this dataset:

Dataset
Date                TotalPartsCreatedDuringDate
7/1/2012         2
7/2/2012         1
7/3/2012         3

How would be build a linq query to get this?
0
Comment
Question by:adskarcox
2 Comments
 
LVL 13

Expert Comment

by:agarwalrahul
ID: 37780371
var q = from i in ABD.Listitem
        let dt = p.EffectiveDate
        group i by new { y = dt.Year, m = dt.Month, d = dt.Day}
        select g;

Reference:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/cb58ae5f-5db9-4054-92b6-a1bf63764574
0
 
LVL 10

Accepted Solution

by:
eguilherme earned 500 total points
ID: 37781273
check this:

var TotalByDate = (from t in Part.getSamples()
		   group t by t.DateTimeCreated.Date into n
		   select new
		   {
			   Date = n.Key.Date,
			   TotalPartsCreatedDuringDate = n.Count()
		   }).ToList();

Open in new window


and the getSamples method used for this test

public class Part
{
	public int ID {get;set;}
	public string PartName {get;set;}
	public DateTime DateTimeCreated {get;set;}

	public static List<Part> getSamples()
	{
		return new List<Part>()
		{
			new Part() { PartName = "Hammer", ID = 1, DateTimeCreated = new DateTime(2012,1,7,01,22,33) },
			new Part() { PartName = "Shovel", ID = 2, DateTimeCreated = new DateTime(2012,1,7,01,22,33) },
			new Part() { PartName = "Nail", ID = 3, DateTimeCreated = new DateTime(2012,2,7,01,22,33) },
			new Part() { PartName = "Wrench", ID = 4, DateTimeCreated = new DateTime(2012,3,7,01,22,33) },
			new Part() { PartName = "Screw", ID = 5, DateTimeCreated = new DateTime(2012,3,7,01,22,33) },
			new Part() { PartName = "Pliers", ID = 6, DateTimeCreated = new DateTime(2012,3,7,01,22,33) }
		};
	}
}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now