Solved

C# Generic Method Parameter

Posted on 2010-09-20
4
547 Views
Last Modified: 2012-05-10
Hello,

Is there a way to make the method below generic?  

CustomerData is a strongly-typed dataset.  I would like to be able to somehow pass a generic strongly-typed dataset in as a parameter or however this can be done so that we only use one method for all instances.  Otherwise we will need to duplicate this method about 10 times with different dataset types.

We would also pass in the table name as string for the dataset.

Thanks.
public string[] GetCustomerSuggestionList(string columnName, string prefix, int count)
        {
 		CustomerData customerData = new CustomerData();
                customerData = GetCustomersAll();

              	EnumerableRowCollection<DataRow> query = from customer in customerData.Tables["customer"].AsEnumerable()
                        where customer.Field<string>(columnName) != null && customer.Field<string>(columnName).ToLower().StartsWith(prefix.ToLower())
                        select customer;
                     
 		UtilityBF utilityBF = new UtilityBF();
                return utilityBF.GetDistinctQueryItems(query, columnName, count);
        }

Open in new window

0
Comment
Question by:kruegerste
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 33718590
Something like this?
public string[] GetCustomerSuggestionList<T>(T dataSet, string tableName, string columnName, string prefix, int count)
    where T : DataSet
{
    EnumerableRowCollection<DataRow> query = from customer in dataSet.Tables[tableName].AsEnumerable()
                                             where customer.Field<string>(columnName) != null && customer.Field<string>(columnName).ToLower().StartsWith(prefix.ToLower())
                                             select customer;

    UtilityBF utilityBF = new UtilityBF();
    return utilityBF.GetDistinctQueryItems(query, columnName, count);
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33718630
Inferred usage:
public void SomeSub()
{
    CustomerData customerData = GetCustomersAll();

    string[] result = GetCustomerSuggestionList<CustomerData>(customerData, "customer", "address", "something", 4);
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:kruegerste
ID: 33719271
Thanks.  I was missing the where statement.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33719356
NP. Glad to help  :)
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question