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

  • Home
  • SEARCH
  • 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 8846381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:58:12+00:00 2026-06-14T11:58:12+00:00

I have the following code. All 20 objects seem to get created ok. The

  • 0

I have the following code.
All 20 objects seem to get created ok.
The first foreach then works fine and iterates through all 20.
The second example using linq works well aswell.

Is it then possible to target just one of the objects, using a property such as ReportKey, and run the method RunThisReport just for that object? Or, because I’ve used the type IEnumerable<> have I gone down a dead-end?

static void Main(string[] args) {

    var models = SelectReports("SELECT * FROM 20RecordTable");

     //1.running the method for each
    foreach(myReport x in models) {
        Console.WriteLine("Doubled key:{0}",  x.myReportKeyDoubled().ToString());
    }

    //2.linq sample
    var result = from sample in models
                    select sample.ReportName;
    foreach(string x in result) {
        Console.WriteLine(Convert.ToString(x));
    }

    //3. target a single report say reportKey 512 and run the method RunThisReport?

    Console.WriteLine("Press [enter] to exit");
    Console.Read();
}

static IEnumerable<myReport> SelectReports(string myCommandText) {

    var connectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;
    using(var conn = new SqlConnection(connectionString))
    using(var cmd = conn.CreateCommand()) {
        conn.Open();
        cmd.CommandText = myCommandText;
        using(var reader = cmd.ExecuteReader()) {

            while(reader.Read()) {
                yield return new myReport {

                    ReportKey = reader.GetInt32(reader.GetOrdinal("ReportKey")),
                    ReportName  = reader.GetString(reader.GetOrdinal("ReportName")),
                    ReportDescription  = reader.GetString(reader.GetOrdinal("ReportDescription")),
                    ReportTechDescription = reader.GetString(reader.GetOrdinal("ReportTechDescription "))

                };
            }
        }
    }
}

public class myReport {

    public int ReportKey { get; set; }
    public string ReportName { get; set; }
    public string ReportDescription { get; set; }
    public string ReportTechDescription { get; set; }

    public int myReportKeyDoubled() {
        return ReportKey*2;
    }
    public string RunThisReport(){
        return this.ReportName + " needs to be run via" + this.ReportTechDescription;
    }         
}
  • 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-14T11:58:13+00:00Added an answer on June 14, 2026 at 11:58 am
     var report = models.SingleOrDefault(m => m.ReportKey == 512);
    
     if (report != null)
         report.RunThisReport();
    

    Or with comprehension syntax (ugly, yes?):

    var report = (from m in models
                  where m.ReportKey == 512
                  select m).SingleOrDefault();
    

    BTW with Dapper your code will look like:

    static IEnumerable<myReport> SelectReports(string myCommandText) 
    {    
        var connectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;
        using(var conn = new SqlConnection(connectionString))
        {
            conn.Open();
            return conn.Query<myReport>(myCommandText);    
        }
    }
    

    Dapper is available via NuGet. And it’s my choice when working with ADO.NET

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

Sidebar

Related Questions

i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
Suppose I have the following code to collect all the possible combinations. abArray =
So I have the following code for fetching all of my core data entities
I have the following code to assign a value to all the elements of
I have this following code which will return all the current semesters. How do
I have the below code that does not seem to work at all :(
I have tried to edit my code to the following But dont seem to
I have the following code: NSLog(@items: %d, [items count]); NSLog(@allObjects: %d, [self.allObjects count]); [self.allObjects
I have the following code: function build_all_combinations(input_array){ array = [1,2,3] limit = array.length -
I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(,) (Essentially I'm trying to build a

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.