How to use Take()
with the following code?
var context = new Entities();
BindingSource bi = new BindingSource();
var TableName = cboSelectTable.Text.ToString();
bi.DataSource = context.GetType().GetProperty(TableName).GetValue(context, null);
Thanks.
You'd need to cast the result of GetValue()
to something appropriate. Is it always going to be a sequence of some class type? If so, as of C# 4 and .NET 4 you could use generic covariance:
var context = new Entities();
var tableName = cboSelectTable.Text.ToString();
var rawData = context.GetType().GetProperty(TableName).GetValue(context, null);
var truncatedData = ((IQueryable<object>) rawData).Take(100);
var source = new BindingSource { DataSource = truncatedData };
You may also want to call ToList()
after Take(100)
, to materialize the query results.
(This is assuming it will be an IQueryable<T>
... if it's just just an IEnumerable<T>
, cast to that instead.)
It's not clear what Entities
is, but it's entirely possible that there's a way of getting the data out without using reflection - if you could give us more information, we could help more.
See more on this question at Stackoverflow