hi folks
I'm having trouble converting the following (functional) SQL statement into Linq
SELECT TOP (100) PERCENT UsrComp, CurrentDate, COUNT(DISTINCT [User]) AS UniqueUsers
FROM dbo.UserLogs
GROUP BY UsrComp, CurrentDate
ORDER BY UsrComp, CurrentDate
USE [Rhonda]
i've tried these 2 attempts with Linq:
having trouble with this (just via 101 LinqPad):from c in UserLogs
group c by new {c.UsrComp, c.CurrentDate} into grp
orderby grp.Key.UsrComp, grp.Key.Currentdate
select new { Comp = grp.Key.UsrComp, Date = grp.Key.CurrentDate, User_Count=grp.Select(x = > x.User).Distinct().Count() }
*and this*:
from c in UserLogs
group c by new {c.UsrComp, c.CurrentDate, c.User} into grp
orderby grp.Key.UsrComp, grp.Key.CurrentDate, grp.User
select new { Comp = grp.Key.UsrComp, Date = grp.Key.CurrentDate, User_Count=grp.Select(x = > grp.Key.User).Distinct().Count() }
definitions:
GO
/****** Object: Table [dbo].[UserLogs] Script Date: 4/18/2014 10:58:23 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserLogs](
[UserLogID] [int] IDENTITY(1,1) NOT NULL,
[UsrComp] [nvarchar](20) NOT NULL,
[User] [nvarchar](20) NOT NULL,
[CurrentDate] [date] NOT NULL,
[TimeRecorded] [datetime] NOT NULL,
CONSTRAINT [PK_UserLogs] PRIMARY KEY CLUSTERED
(
[UserLogID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
data:
1 | ABC | John | 2014-01-01 | 2014-01-01 10:15:00.000 |
2 | ABC | John | 2014-01-01 | 2014-01-01 16:15:00.000 |
3 | ABC | Mary | 2014-01-01 | 2014-01-01 12:15:00.000 |
4 | ABC | John | 2014-01-02 | 2014-01-01 10:15:00.000 |
5 | DEF | Rhonda | 2014-01-02 | 2014-01-01 10:15:00.000 |
6 | DEF | Rob | 2014-01-01 | 2014-01-01 12:15:00.000 |
7 | DEF | Rob | 2014-01-01 | 2014-01-01 16:15:00.000 |
8 | DEF | Rob | 2014-01-01 | 2014-01-01 10:15:00.000 |
Rob K