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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:07:43+00:00 2026-05-26T13:07:43+00:00

switch(ID) { case CustomReportsContainer: foreach (var report in SessionRepository.Instance.CustomReports.ToList()) { var reportItem = new

  • 0
switch(ID)
{
    case "CustomReportsContainer":
        foreach (var report in SessionRepository.Instance.CustomReports.ToList())
        {
            var reportItem = new RadListBoxItem(report.Name, report.ID.ToString());
            if (!Items.Any(item => int.Equals(item.Value, reportItem.Value)))
            {
                Items.Add(reportItem);
            }
        }
        break;
    case "HistoricalReportsContainer":
        foreach (var report in SessionRepository.Instance.HistoricalReports.ToList())
        {
            var reportItem = new RadListBoxItem(report.Name, report.ID.ToString());
            if (!Items.Any(item => int.Equals(item.Value, reportItem.Value)))
            {
                Items.Add(reportItem);
            }
        }
        break;
}

HistoricalReports and CustomReports are collections of different types, but I am interested in the same two properties from each object type. I thought that I should be able to use LINQ’s Select, and create a list of anonymously typed objects.

I can’t create an implicitly typed variable without assigning to it, though. And since I’m inside of a Switch statement’s scope… I can’t assign to var inside of the switch statement, then move the rest of the code outside of the switch statement.

How should I express this code? Is the current implementantion ‘best’?

Uggh, close. Can I do anything with this? It’s through our API so I can’t modify any deeper.

ReportServices.GetAllCustomReports().Select(report => new { report.Name, report.ID} ).ToList().ForEach(customReport => _customReports.Add(customReport));

Error   2   Argument 1: cannot convert from 'AnonymousType#1' to 'CableSolve.Web.Dashboard.IReport'
  • 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-26T13:07:44+00:00Added an answer on May 26, 2026 at 1:07 pm

    CustomReport and HistoricalReport should implement an IReport interface that contains the Id and Name properties (at minimum).

    switch(ID)
    {
        case "CustomReportsContainer":
            AddReportItems(SessionRepository.Instance.CustomReports);
            break;
        case "HistoricalReportsContainer":
            AddReportItems(SessionRepository.Instance.HistoricalReports);
            break;
    }
    
    private void AddReportItems(IEnumerable<IReport> reports)
    {
        foreach (var report in reports)
        {
            var reportItem = new RadListBoxItem(report.Name, report.ID.ToString());
            if (!Items.Any(item => int.Equals(item.Value, reportItem.Value)))
            {
                Items.Add(reportItem);
            }
        }
    }
    

    Edit

    In response to your additional questions, would it be possible for GetAllCustomReports to return a type that implements the IReport interface? This would eliminate the need for projecting to an anonymous type via Select(report => new { report.Name, report.ID} ) and should resolve the remaining issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there instances where switch(case) is is a good design choice (except for simplicity)
I have a switch case like this switch($filesize_in_bytes){ case 10Mb <= $filesize_in_bytes <= 100Mb:
If I have a switch-case statement where the object in the switch is a
We need to identify and then process switch/case statements in code based on some
I have a rather long switch-case statement. Some of the cases are really short
What is the better practice of the following two switch/case statements? Is there an
So I have this problem with strings and switch-case, and I'll try to keep
Is there a CTRL+space -like way of auto-constructing a switch case around a given
I have kept hearing this statement. Switch..Case is Evil for code maintenance, but it
i use visual studi 2008. (c++) in my switch case a wanted to create

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.