Solved

Linq Query Question: Chart Dataset

Posted on 2012-03-28
2
312 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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