public List<ListItem> GetListOfValue(string dataFieldName, string dataFieldValue, string tableName)
{
List<ListItem> listItemList = new List<ListItem>();
ListItem listItem = null;
MetaTable table = _dataContext.Mapping.GetTables().Where(t => t.TableName.ToUpper().Equals("DBO." + tableName.ToUpper())).FirstOrDefault();
if (table == null)
throw new NullReferenceException("table");
ITable iTable = _dataContext.GetTable(table.RowType.Type);
if (iTable == null)
throw new NullReferenceException("iTable");
List<object> objectList = iTable.OfType<object>().ToList();
foreach (object obj in objectList)
{
listItem = new ListItem();
foreach (PropertyInfo property in obj.GetType().GetProperties())
{
if (property.Name.ToUpper().Equals(dataFieldName.ToUpper()))
{
listItem.Text = property.GetValue(obj, null).ToString();
}
if (property.Name.ToUpper().Equals(dataFieldValue.ToUpper()))
{
listItem.Value = property.GetValue(obj, null).ToString();
}
}
listItemList.Add(listItem);
}
return listItemList;
}
Showing posts with label querying linq to entities and sql through reflection. Show all posts
Showing posts with label querying linq to entities and sql through reflection. Show all posts
Sunday, July 10, 2011
Linq to Sql/Entities Table values using Reflection
The following function takes input table name, data field column name and data value column name and returns list of ListItem so developer can use this list in dropdown and other list control. the following function uses reflection to access table and column for values.
Subscribe to:
Posts (Atom)