Dynamic Typed list

Posts   
 
    
Posts: 32
Joined: 01-Oct-2007
# Posted on: 01-Oct-2007 14:48:20   

public List<string> GetDistinctValues(string tableName, string fieldName)
        {
            using (DataAccessAdapter DA = new DataAccessAdapter(_connectionString))
            {

                ResultsetFields fields = new ResultsetFields(3);
                fields.DefineField(CapitalBalanceFields.AccountName, 0);

                DataTable dynamicList = new DataTable();
                DA.FetchTypedList(fields, dynamicList, null, 0, false);

                List<string> distinctValues = new List<string>();
                foreach (DataRow dr in dynamicList.Rows  ) 
                {
                    distinctValues.Add(dr[0].ToString());
                }

                return distinctValues;
            }
        }

Hi all

Is there anyway I can modify the above function so that the "CapitalBalanceFields.AccountName" section uses the fields from the passed in entity names, rather than them having to be hard coded...?

thanks

Matt

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 01-Oct-2007 17:46:10   

Hi, I haven't tried but I guess it should work, let me know if it was helpful. By the way, I assume you obtain the field list in a comma separated value string parameter.


       public List<string> GetDistinctValues(string tableName, string fieldName)
        {           
            using (DataAccessAdapter DA = new DataAccessAdapter(_connectionString))
            {
                string[] sFields = fieldName.Split(',');
                ResultsetFields fields = new ResultsetFields(sFields.Length);
                int i = 0;
                foreach (string f in sFields)
                {
                    fields.DefineField(EntityFieldFactory.Create(tableName, f), i++);                   
                }                               

                DataTable dynamicList = new DataTable();
                DA.FetchTypedList(fields, dynamicList, null, 0, false);

                List<string> distinctValues = new List<string>();
                foreach (DataRow dr in dynamicList.Rows)
                {
                    distinctValues.Add(dr[0].ToString());
                }

                return distinctValues;
            }
        }