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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:14:34+00:00 2026-05-20T08:14:34+00:00

Hey All, I am a newbie in C# and curious about the better solution

  • 0

Hey All,
I am a newbie in C# and curious about the better solution of my case.

I have a method which gets the DataTable as a parameter and creates a List with MyClass’s variables and returns it.

public static List<Campaigns> GetCampaignsList(DataTable DataTable)
    {
        List<Campaigns> ListCampaigns = new List<Campaigns>();

        foreach (DataRow row in DataTable.Rows)
        {
            Campaigns Campaign = new Campaigns();

            Campaign.CampaignID = Convert.ToInt32(row["CampaignID"]);
            Campaign.CustomerID = Convert.ToInt32(row["CustomerID"]);
            Campaign.ClientID = Convert.ToInt32(row["ClientID"]);
            Campaign.Title = row["Title"].ToString();
            Campaign.Subject = row["Subject"].ToString();
            Campaign.FromName = row["FromName"].ToString();
            Campaign.FromEmail = row["FromEmail"].ToString();
            Campaign.ReplyEmail = row["ReplyEmail"].ToString();
            Campaign.AddDate = Convert.ToDateTime(row["AddDate"]);
            Campaign.UniqueRecipients = Convert.ToInt32(row["UniqueRecipients"]);
            Campaign.ClientReportVisible = Convert.ToBoolean(row["ClientReportVisible"]);
            Campaign.Status = Convert.ToInt16(row["Status"]);

            ListCampaigns.Add(Campaign);
        }

        return ListCampaigns;
    }

And one of my another DataTable method gets the DataTable from the database with given parameters. Here is the method.

public static DataTable GetNewCampaigns()
    {
        DataTable dtCampaigns = new DataTable();

        Campaigns Campaigns = new Campaigns();
        dtCampaigns = Campaigns.SelectStatus(0);

        return dtCampaigns;
    }

But the problem is that, this GetNewCampaigns method doesnt take parameters but other methods can take parameters. For example when I try to select a campaign with a CampaignID, I have to send CampaignID as parameter. These all Database methods do take return type as DataTable but different number of parameters.

public static DataTable GetCampaignDetails(int CampaignID)
    {
        DataTable dtCampaigns = new DataTable();

        Campaigns Campaigns = new Campaigns();
        dtCampaigns = Campaigns.Select(CampaignID);

        return dtCampaigns;
    }

At the end, I want to pass a Delegate to my first GetCampaignList Method as parameter which will decide which Database method to invoke. I dont want to pass DataTable as parameter as it is newbie programming.

Could you pls help me learn some more advance features.
I searched over it and got to Func<> delegate but could not come up with a solution.

  • 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-20T08:14:35+00:00Added an answer on May 20, 2026 at 8:14 am

    It looks like you have two duplicate methods, with the only difference being a specific operation on an object (in this case a method call on Campaigns that returns a DataTable). You’re trying (I think) to refactor this into one method, with that one operation abstracted. This is a perfect job for a lambda expression.

    public static DataTable GetCampaignInSomeWay(Func<Campaigns, DataTable>  CampaingsDelegate) 
    {
       DataTable dtCampaigns = new DataTable();
    
       Campaigns Campaigns = new Campaigns();
       dtCampaigns = CampaingsDelegate(Campaigns);
    
       return dtCampaigns;
    }
    

    And then call it thusly:

    GetCampaignInSomeWay(C => C.Select(someCampaignId));
    

    or

    GetCampaignInSomeWay(C => C.SelectStatus(0));
    

    The family of Action and Func delegates are extremely useful for things like this. Here are some of the docs. Note that both Action and Func have multiple generic overloads

    http://msdn.microsoft.com/en-us/library/bb549151.aspx

    http://msdn.microsoft.com/en-us/library/018hxwa8.aspx

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

Sidebar

Related Questions

hey all, i have a question about urlmapping in grails. I'm trying to make
Hey all, this is a newbie ASP.NET MVC question. I have a view that
hey all.i'm newbie at this problem.i have this data in table result: item range_code
Hey all. Here is the scenario. I have the below code, and I'm needing
Hey, I'm very newbie to xPath and time is against me, I don't have
hey all my question about how the developers makes their interface the buttons, slider,
Hey all i have two JFrames one is my main login frame, user enters
Hey all- I have looked this up on here and Google but none of
Hey all you ruby on railers... I'm just a newbie as of right now..
Hey all here are my tables i have placed in a DIV for each.

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.