?
Solved

C# Generic Method Parameter

Posted on 2010-09-20
4
Medium Priority
?
552 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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