Connecting Tech Pros Worldwide Forums | Help | Site Map

linq group by month problem

Seb
Guest
 
Posts: n/a
#1: Jul 17 '08
I want to count activity in a given month. I'm trying to do so with
the linq code below however it reports:
Error 1 'a' is inaccessible due to its protection level

var ActivityByMonths = from a in db.ActivityDatas
where a.BeginDateTime >= From && a.BeginDateTime <= To
where a.SubActivityId == sat
group a by a.BeginDateTime.Month into mg
select new { a.BeginDateTime.Month, count = mg.Count() };

I want to select is the month number and the number of activities that
took place in that month.

best regards,
seb

Pavel Minaev
Guest
 
Posts: n/a
#2: Jul 17 '08

re: linq group by month problem


On Jul 17, 5:39*pm, Seb <sebastianthegreat...@gmail.comwrote:
Quote:
I want to count activity in a given month. I'm trying to do so with
the linq code below however it reports:
Error * 1 * * * 'a' is inaccessible due to its protection level
>
* * * * * * * * var ActivityByMonths = from a in db.ActivityDatas
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where a.BeginDateTime >= From && a.BeginDateTime <= To
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where a.SubActivityId ==sat
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *group a by a.BeginDateTime.Month into mg
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *select new { a.BeginDateTime..Month, count = mg.Count() };
>
I want to select is the month number and the number of activities that
took place in that month.
Once you group 'a', it goes out of scope (since there's no single 'a'
associated with each group). The reason for the confusing error
message you get is probably that you have another 'a' in scope, which
is indeed inaccessible (most likely, a field with such name in a base
class). Instead, to access the value of the grouping key for each
group, use mg.Key:

* * * * * * * * var ActivityByMonths = from a in db.ActivityDatas
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where
a.BeginDateTime >= From && a.BeginDateTime <= To
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where
a.SubActivityId == sat
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *group a by
a.BeginDateTime.Month into mg
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *select new
{ mg.Key, count = mg.Count() };
Seb
Guest
 
Posts: n/a
#3: Jul 17 '08

re: linq group by month problem


On Jul 17, 4:05*pm, Pavel Minaev <int...@gmail.comwrote:
Quote:
On Jul 17, 5:39*pm, Seb <sebastianthegreat...@gmail.comwrote:
>
Quote:
I want to count activity in a given month. I'm trying to do so with
the linq code below however it reports:
Error * 1 * * * 'a' is inaccessible due to its protection level
>
Quote:
* * * * * * * * var ActivityByMonths = from a in db.ActivityDatas
* * * * * * * * * * * * * * * * * ** * * * * * * * * * * *where a.BeginDateTime >= From && a.BeginDateTime <= To
* * * * * * * * * * * * * * * * * ** * * * * * * * * * * *where a.SubActivityId == sat
* * * * * * * * * * * * * * * * * ** * * * * * * * * * * *group a by a.BeginDateTime.Month into mg
* * * * * * * * * * * * * * * * * ** * * * * * * * * * * *select new { a.BeginDateTime.Month, count = mg.Count() };
>
Quote:
I want to select is the month number and the number of activities that
took place in that month.
>
Once you group 'a', it goes out of scope (since there's no single 'a'
associated with each group). The reason for the confusing error
message you get is probably that you have another 'a' in scope, which
is indeed inaccessible (most likely, a field with such name in a base
class). Instead, to access the value of the grouping key for each
group, use mg.Key:
>
** * * * * * * * var ActivityByMonths = from a in db.ActivityDatas
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where
a.BeginDateTime >= From && a.BeginDateTime <= To
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where
a.SubActivityId == sat
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *group a by
a.BeginDateTime.Month into mg
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *select new
{ mg.Key, count = mg.Count() };
That gives me another question. Because in that case the code only
works if we are dealing with only one year. In my case the period
might be spanning several years. So I need to select both the year and
the month.

In the end I display the data in a graph with month and year on the x-
axis and number of activities on the y-axis... so being able to select
both month and year is a must.
Seb
Guest
 
Posts: n/a
#4: Jul 17 '08

re: linq group by month problem


On Jul 17, 8:35*pm, Seb <sebastianthegreat...@gmail.comwrote:
Quote:
On Jul 17, 4:05*pm, Pavel Minaev <int...@gmail.comwrote:
>
>
>
Quote:
On Jul 17, 5:39*pm, Seb <sebastianthegreat...@gmail.comwrote:
>
Quote:
Quote:
I want to count activity in a given month. I'm trying to do so with
the linq code below however it reports:
Error * 1 * * * 'a' is inaccessible due to its protection level
>
Quote:
Quote:
* * * * * * * * var ActivityByMonths = from a in db..ActivityDatas
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where a.BeginDateTime >= From && a.BeginDateTime <= To
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where a.SubActivityId == sat
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *group a by a.BeginDateTime.Month into mg
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *select new { a.BeginDateTime.Month, count = mg.Count() };
>
Quote:
Quote:
I want to select is the month number and the number of activities that
took place in that month.
>
Quote:
Once you group 'a', it goes out of scope (since there's no single 'a'
associated with each group). The reason for the confusing error
message you get is probably that you have another 'a' in scope, which
is indeed inaccessible (most likely, a field with such name in a base
class). Instead, to access the value of the grouping key for each
group, use mg.Key:
>
Quote:
** * * * * * * * var ActivityByMonths = from a in db.ActivityDatas
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where
a.BeginDateTime >= From && a.BeginDateTime <= To
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *where
a.SubActivityId == sat
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *group a by
a.BeginDateTime.Month into mg
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *select new
{ mg.Key, count = mg.Count() };
>
That gives me another question. Because in that case the code only
works if we are dealing with only one year. In my case the period
might be spanning several years. So I need to select both the year and
the month.
>
In the end I display the data in a graph with month and year on the x-
axis and number of activities on the y-axis... so being able to select
both month and year is a must.
var ActivityByMonths = from a in db.ActivityDatas
where a.BeginDateTime >= From && a.BeginDateTime <= To
where a.SubActivityId == sat
group a by a.BeginDateTime.Month.ToString() + " " +
a.BeginDateTime.Year.ToString() into mg
select new { Month = mg.Key, Count = mg.Count() };

To my surprise the above code works but I feel it's a bit... fishy.
And a problem is that I wanted to display the month by name rather
then number.
I tried with
group a by a.BeginDateTime.ToString("M y") into mg
but that gives me: Method 'System.String ToString(System.String)' has
no supported translation to SQL.

Any ideas?
Marc Gravell
Guest
 
Posts: n/a
#5: Jul 18 '08

re: linq group by month problem


Well, you can probably do some things with the culture's calendar -
but what you have is simple. Note the difference here is in the SQL
query; using the above the SQL at the database is simple - allowing
your scalable system (the app-servers) to do the formatting of numbers
to strings. Which is what we want ;-p

Marc
Seb
Guest
 
Posts: n/a
#6: Jul 18 '08

re: linq group by month problem


I guess you are right. Thanks a lot for the help.


seb
Closed Thread


Similar C# / C Sharp bytes