PC Review


Reply
Thread Tools Rate Thread

How do I do this in Linq

 
 
David
Guest
Posts: n/a
 
      10th Apr 2009
Hi,

Sample query... how do I write it in linq?

SELECT UserName, COUNT(UserName) AS Expr1
FROM aspnet_Users
WHERE (LastActivityDate BETWEEN '23 March 2009' AND '10 April 2009')
GROUP BY UserName

I have tried...


var users = from Users in dc.aspnet_Users
where Users.LastActivityDate >= "23 March 2009"
where Users.LastActivityDate <= "10 April 2009"
group Users by Users.UserName into g
select new
{
Key = g.Key, Count = g.Count()
};

but it doesn't work. (problem with >= and <=)

I am new to linq (still attempting to get my head around it). I am not even
sure if g.Key and g.Count() are what I am looking for. All I know is I want
to be able to return a resultset like the sample query.

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      10th Apr 2009
aaarrrggghhh!!!

When I read the error message properly, I found the fix. Doh!!!

Convert.ToDateTime() of my date strings.

However, I have key and count, what if I want to select more items in my
initial select or have further counts?

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


"David" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi,
>
> Sample query... how do I write it in linq?
>
> SELECT UserName, COUNT(UserName) AS Expr1
> FROM aspnet_Users
> WHERE (LastActivityDate BETWEEN '23 March 2009' AND '10 April 2009')
> GROUP BY UserName
>
> I have tried...
>
>
> var users = from Users in dc.aspnet_Users
> where Users.LastActivityDate >= "23 March 2009"
> where Users.LastActivityDate <= "10 April 2009"
> group Users by Users.UserName into g
> select new
> {
> Key = g.Key, Count = g.Count()
> };
>
> but it doesn't work. (problem with >= and <=)
>
> I am new to linq (still attempting to get my head around it). I am not
> even sure if g.Key and g.Count() are what I am looking for. All I know is
> I want to be able to return a resultset like the sample query.
>
> --
> Best regards,
> Dave Colliver.
> http://www.AshfieldFOCUS.com
> ~~
> http://www.FOCUSPortals.com - Local franchises available
>



 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      10th Apr 2009
David wrote:

> However, I have key and count, what if I want to select more items in my
> initial select or have further counts?


counts of what exactly? Currently, with

var users = from Users in dc.aspnet_Users

group Users by Users.UserName into g
select new
{
Key = g.Key, Count = g.Count()
};

you count the items in each group g.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
David
Guest
Posts: n/a
 
      10th Apr 2009
so, if I do something like...

group Users by Users.UserName into g
group Users by Users.LoweredUserName in h

I can then do h.Key and h.Count() as well as g.Key and g.Count() in select?

(I am getting a bit ahead of myself here... but is the theory there?)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Martin Honnen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> David wrote:
>
>> However, I have key and count, what if I want to select more items in my
>> initial select or have further counts?

>
> counts of what exactly? Currently, with
>
> var users = from Users in dc.aspnet_Users
>
> group Users by Users.UserName into g
> select new
> {
> Key = g.Key, Count = g.Count()
> };
>
> you count the items in each group g.
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
populate a DataSet via a LINQ query (Linq to XML) Anthony Microsoft C# .NET 12 19th Apr 2010 06:49 AM
Linq to XML--Are there code examples that make Linq as easy as SQL? Or how can I convert ths simple pseudo code into real code? Reece Microsoft C# .NET 4 10th Dec 2008 03:13 AM
Mixing linq to sql and linq to xml (in linqpad) timor.super@gmail.com Microsoft C# .NET 2 11th Jul 2008 12:05 PM
Linq to SQL - Return DataTable as a result of Linq query szwejk Microsoft C# .NET 3 1st Feb 2008 03:07 PM
In Orcas Linq, When does it happen to convert LINQ to SQL command =?Utf-8?B?UGV0ZXI=?= Microsoft ADO .NET 0 30th Aug 2007 02:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:44 PM.