Solved

C# Generic Method Parameter

Posted on 2010-09-20
4
551 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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: …

688 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