given this code
namespace GridTests
{
public class Grid<T>
{
IEnumerable<T> DataSource { get; set; }
IList<Column> Columns = new List<Column>();
class Column
{
public string DisplayText { get; set; }
Func<T, object> Rowdata { get; set; }
}
}
}
I need to be able to loop through the columns collection and get the 开发者_运维问答Rowdata's object value using the DisplayText.
Thanks
IEnumerable<object> Search(string yourLookup)
{
foreach(Column column in yourGridInstance.Columns)
{
if(column.DisplayText.Equals(yourLookup, yourStringcomparaisonOptions)
{
foreach(T value in yourGridInstance.DataSource)
{
yield return column.Rowdata(T);
}
}
}
}
Linq version
IEnumerable<object> Search(string yourLookup)
{
return yourGridInstance.Columns
.Where(column=>column.DisplayText.Equals(yourLookup, yourStringcomparaisonOptions))
.SelectMany(c=> yourGridInstance.DataSource.Select(data=>c.RowData(data)));
}
Your question is still not very clear. I am not sure which T you want to supply the RowData method. If you want to run the method with every T in your DataSource enumerator, you could do something like this.
public void Enumerate(string displayText)
{
Column column = this.Columns.FirstOrDefault(item => item.DisplayText == displayText);
if (column != null)
{
foreach (T key in DataSource)
{
object value = column.Rowdata(key);
// Do something with your value here.
}
}
else
{
throw new ArgumentException("DisplayText not found in Columns.", "displayText");
}
}
精彩评论