Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7077323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:24:33+00:00 2026-05-28T06:24:33+00:00

I have this code // was before var groupTransactions; IEnumerable<IGrouping<TransactionsGroupedByMonthYear, TransactionDto>> groupTransactions = null;

  • 0

I have this code

   // was before var groupTransactions;
   IEnumerable<IGrouping<TransactionsGroupedByMonthYear, TransactionDto>> groupTransactions = null;

DollarCapPeriod dollarCapPeriod = (DollarCapPeriod)Enum.Parse(typeof(DollarCapPeriod), reward.DollarCapPeriod);
switch (dollarCapPeriod)
{
    case DollarCapPeriod.Monthly:
        groupTransactions = filterdTransactions.GroupBy(x => new { x.TransactionDate.Month, x.TransactionDate.Year });
        break;
    case DollarCapPeriod.Yearly:
  groupTransactions = filterdTransactions.GroupBy(x => new TransactionsGroupedByMonthYear { Month = 0, Year = x.TransactionDate.Year });
        break;
    default:
        break;
}

I want to do something like that but it wants the type initialized. So I am wondering is there away around this or do I have to make some sort of concrete type? If so how do I do it again with groupBy?

Sample Data

1/14/2012,5,Gas
1/15/2012,5,Gas
1/16/2012,5,Gas
1/17/2012,5,Gas

Edit

Currently I am trying to do now use a concrete backing but I need to filter by 2 different ways. Sometimes I need only the “Year” and some times I need “Month” and “Year”

I don’t know how to do this. They both need to go in the same variable so they need to be the same type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:24:34+00:00Added an answer on May 28, 2026 at 6:24 am

    First of all, don’t confuse anonymous types with inferred types. The var keyword does not necessarily mean a type is anonymous. An anonymous type does not have a name as far as the developer is concerned (the compiler, of course, gives it one, but that’s an implementation detail that you can and should ignore). An inferred type, using the var keyword, could have a name, you just didn’t have to type it.

    It is never allowed to say:

    var x;
    

    because there is no information from which to infer the type.

    In the general case, you can use anonymous types in scoping situations like loops, try/catch, etc. by initializing a new instance of the anonymous type ahead of time:

    var x = new { Month = 0, Year = 0 };
    switch (foo)
    {
      case 1:
        x = new { transaction.Month, transaction.Year };
    }
    

    As long as the field names and types match between the two anonymous type initializers, the “actual” type is the same.

    In your specific case, though, you don’t want just an anonymous type, because GroupBydoesn’t returning an anonymous type. It returns an IEnumerable<IGrouping<TKey, TSource>>, and in your case only TKey is anonymous. While it is possible to declare that type in code, it’s very hard, and would involve temporary variables being declare solely to get their anonymous typeof() information out of them. It would almost certainly be an unmaintainable mess.

    Instead you should just declare a type to use at your group key. It’s still going to be ugly, on account of the nested generics, but nowhere near as bad as trying to use the anonymous type.

    public class TransactionGroup
    {
      public int Month;
      public int Year;
    }
    

    and get your grouping:

    IEnumerable<IGrouping<TransactionGroup, Transaction>> groupTransaction;
    var period = (Period)Enum.Parse(typeof(Period), record.Period);
    switch (period)
    {
        case Period.Monthly:
            groupTransaction = transactions.GroupBy(x => new TransactionGroup
              {
                Month = x.TransactionDate.Month, 
                Year = x.TransactionDate.Year 
              });
            break;
        case Period.Yearly:
            groupTransaction = transactions.GroupBy(x => new TransactionGroup
              {                 
                Month = 0, 
                Year = x.TransactionDate.Year 
              });
            break;
        default:
            break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code so I can grab the values ind the dataset before
I have used code like this: http://msdn.microsoft.com/en-us/library/dw70f090.aspx to access database before when working in
Ok, I have code that is working (posted after this) but before I expand
I have this code which I need to set a unique title : var
i have this code but i dont know what to do next.. var toBeInsertedToAS
I have this code: <script> function EvalSound(soundobj) { var thissound=document.getElementById(soundobj); thissound.Play(); } </script> <embed
I have this code: <script type=text/javascript> var loader = #loader; $(function() { $(#selUsers).change(function() {
I have this jquery code : $(document).ready(function() { $('div.badge_button > .more').click(function() { var id
Possible Duplicate: Can someone Explain this jQuery code? I have posted this before, but
I have some code like this. Should the break occur before the periods or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.