By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

linq group by month problem

P: n/a
Seb
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
Jul 17 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jul 17, 5:39*pm, Seb <sebastianthegreat...@gmail.comwrote:
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() };
Jul 17 '08 #2

P: n/a
Seb
On Jul 17, 4:05*pm, Pavel Minaev <int...@gmail.comwrote:
On Jul 17, 5:39*pm, Seb <sebastianthegreat...@gmail.comwrote:
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() };
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.
Jul 17 '08 #3

P: n/a
Seb
On Jul 17, 8:35*pm, Seb <sebastianthegreat...@gmail.comwrote:
On Jul 17, 4:05*pm, Pavel Minaev <int...@gmail.comwrote:
On Jul 17, 5:39*pm, Seb <sebastianthegreat...@gmail.comwrote:
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() };

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?
Jul 17 '08 #4

P: n/a
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
Jul 18 '08 #5

P: n/a
Seb
I guess you are right. Thanks a lot for the help.
seb
Jul 18 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.