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 8127885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:41:28+00:00 2026-06-06T07:41:28+00:00

I want to dynamically add .Select statements, but the compiler will only allow me

  • 0

I want to dynamically add .Select statements, but the compiler will only allow me to build these statements inside the current scope. I am aware of linqkit, which could be a solution, but i dont want to add any external functionallity. How do i avoid this code duplication which i have now:

 if (request.Equals("some text"))
        {
            var filter = commonFilter.GroupBy(x => new
                {
                    x.Timestamp.Year,
                    x.Timestamp.Month,
                })
                .Select(g => new
                {
                    Year = g.Key.Year,
                    Month = g.Key.Month,
                    TotErr = g.Count(),

                    column1 = g.Sum(o => o.Process.Contains("something") ? 1 : 0),
                    column2= g.Sum(o => o.Process.Contains(".something1") ? 1 : 0),
                    column3= g.Sum(o => o.Process.Contains(".something2") ? 1 : 0),
                    column4= g.Sum(o => o.Process.Contains("something3") ? 1 : 0),
                    column5= g.Sum(o => o.Process.Contains(".something4") ? 1 : 0),
                    column6 = g.Sum(o => o.Process.Contains(".something5") ? 1 : 0),
                    column7= g.Sum(o => o.Process.Contains(".something6") ? 1 : 0),
                    column8= g.Sum(o => o.Process.Contains(".something7") ? 1 : 0),
                    column9= g.Sum(o => o.Process.Contains(".something8") ? 1 : 0),

                    NumOrgs = g.Select(l => l.OrganizationId).Distinct().Count(),
                    NumUsers = g.Select(l => l.UserId).Distinct().Count(),

                });

            return filter.AsPa......;//not important right now
        }
        //Same thing again but slightly different
        else if (request.Equals("some other text"))
        {
            var filter = commonFilter.GroupBy(x => new
                {
                    x.Timestamp.Year,
                    x.Timestamp.Month,
                    x.Timestamp.Day
                })
                .Select(g => new
                {
                    Year = g.Key.Year,
                    Month = g.Key.Month,
                    Day= g.Key.Day,
                    TotErr = g.Count(),

                    column1 = g.Sum(o => o.Process.Contains("something") ? 1 : 0),
                    column2= g.Sum(o => o.Process.Contains(".something1") ? 1 : 0),
                    column3= g.Sum(o => o.Process.Contains(".something2") ? 1 : 0),
                    column4= g.Sum(o => o.Process.Contains("something3") ? 1 : 0),
                    column5= g.Sum(o => o.Process.Contains(".something4") ? 1 : 0),
                    column6 = g.Sum(o => o.Process.Contains(".something5") ? 1 : 0),
                    column7= g.Sum(o => o.Process.Contains(".something6") ? 1 : 0),
                    column8= g.Sum(o => o.Process.Contains(".something7") ? 1 : 0),
                    column9= g.Sum(o => o.Process.Contains(".something8") ? 1 : 0),

                    NumOrgs = g.Select(l => l.OrganizationId).Distinct().Count(),
                    NumUsers = g.Select(l => l.UserId).Distinct().Count(),

                });

            return filter.AsPa......;//not important right now
        }
  • 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-06-06T07:41:31+00:00Added an answer on June 6, 2026 at 7:41 am

    If you want to maintain the Anonymous type you can use the InternalsVisibleToAttribute to allow a separate common library to work with the anonymous type.

    I have done this previously to make a generic filtering library for datagrids with EF data sources, but it does involve a level of reflection and building the expression out of generic System.Linq.Expression components so this may not be worthwhile unless you have very many instances you want to do this to.

    See msdn info:

    http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

    and

    http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.aspx

    If the select pattern is more predicable you may not need the expression portion, but in that case it may ultimately be simpler to just use a public class instead of the anonymous one.

    Edit: “cannot convert anonymous to x” type errors mean you haven’t gotten rid of the anonymous entirely/correctly.

    In each of the locations you have new { …whatever… } you are creating an anonymous type. If you create classes for each of those that have the correct properties to match what you are assigning then you just change something that looks like

    new {x=1,y=2}
    

    to read

    new somenewclass() {x=1,x=2}
    

    assuming you have defined elsewhere

    public class somenewclass
    {
        public int x {get;set;}
        public int y {get;set;}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select box that gets generated dynamically. I want to allow users
I want a to add an option dynamically to a Select element using Prototype.
Suppose you want to dynamically add to a HTML menu, where the menu HTML
I'm facing a strange issue using the Criteria API. I want to dynamically add
I want to give users the possibility to dynamically add new columns at the
i want to add Layout Dynamically on Add button click and on Dynamic Layout
Dynamically I want to edit/update hosts(etc/hosts) file to add domain. To edit hosts(etc/hosts) file
HI.. i want to add elements dynamically to listview in QT for symbian OS,
I want to add class atttributes to a superclass dynamically. Furthermore, I want to
I want to add three labels into a cell dynamically, cell is created dynamically

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.