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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:08:20+00:00 2026-05-26T17:08:20+00:00

I am facing one issue with linq query in c# , my linq query

  • 0

I am facing one issue with linq query in c# , my linq query as per below

list = (from row in dt.AsEnumerable()
                            select new Perfmon
                            {
                                id = row.Field<long>("id"),
                                counter1 = row.Field<string>("counter"),

                            }).ToList();

I have one perfmon class and it contains properties like (id, counter1, counter2 …)
there are more then 20 counters now i have developed sql query to select id and counter based on passed countername in parameter e.g. if i have passed counter1 then it will select id , counter1 (renamed as counter) only

if i will use switch case here then it will have 20 witch case, can anyone please help me how can bind property dynamically in linq?

Thanks in advance.

  • 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-26T17:08:21+00:00Added an answer on May 26, 2026 at 5:08 pm

    You can make your Perfmon class backed by a dictionary rather than fields per properties. like:

    class Perfmon
    {
        private readonly Dictionary<string, string> _counters = new Dictionary<string, string>();
    
        public Perfmon(params KeyValuePair<string, string>[] knownCounters)
        {
            foreach (var knownCounter in knownCounters)
            {
                SetCounter(knownCounter.Key, knownCounter.Value);
            }
        }   
        public void SetCounter(string name, string value)
        {
            _counters[name] = value;
        }
    
        protected string GetCounterValue(string name)
        {
            if (_counters.ContainsKey(name))
                return _counters[name];
            else
                return null;
        }
    
        public string Counter1 { get { return GetCounterValue("Counter1"); } }
        public string Counter2 { get { return GetCounterValue("Counter2"); } }
        public string Counter3 { get { return GetCounterValue("Counter3"); } }
    }
    

    The constructor is there so you can easily use it in your query like:

    var counterName = "Counter2";
    list = (from row in dt.AsEnumerable() 
                                select new Perfmon(new KeyValuePair<string, string>(counterName, row.Field<string>("counter")))
                                { 
                                    id = row.Field<long>("id")
                                }).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a problem. I have one query Select * from tabA where
I am new to MVC and facing one issue. I have a xml file
I am facing one issue regarding Layout of detail view in UISplitviewController. below is
We are facing one issue directly connected with our Flash API we've given to
I am facing one issue regarding one module let me clear the flow for
I am facing an issue regarding EF code first version while mapping one entity
I am facing an issue that is driving me insane. I have one and
I'm facing the same issue others already posted on SO: On reading objects from
I am facing one small issue. I want to change the parent div style
I am trying to debug an issue one of my testers is facing. The

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.