也许问题标题不正确。我有以下变量。
注意,我无法调用PerformQuery方法,因为变量y是一个System.Type类型。换句话说,使用PerformQuery(x)或PerformQuery(x)会报编译错误。
编辑:
我遇到这个问题的原因是:我有一个web服务,在其中发送两个参数。一个是我想查询的表的类型(例如 typeof(Customer)),另一个是实际的查询字符串,例如“Select * from customers”。
IEnumerable x = // some IEnumerable
System.Type y = // some type
如何迭代x以生成类型为y的数组?
当我查看互联网时,我发现:
public T[] PerformQuery<T>(IEnumerable q)
{
T[] array = q.Cast<T>().ToArray();
return array;
}
注意,我无法调用PerformQuery方法,因为变量y是一个System.Type类型。换句话说,使用PerformQuery(x)或PerformQuery(x)会报编译错误。
编辑:
我遇到这个问题的原因是:我有一个web服务,在其中发送两个参数。一个是我想查询的表的类型(例如 typeof(Customer)),另一个是实际的查询字符串,例如“Select * from customers”。
protected void Page_Load(object sender, EventArgs e)
{
// code to deserialize posted data
Type table = // implement that here
String query = // the query that was posted
// note DB is of type DbContext
IEnumerable q = Db.Database.SqlQuery(table, query );
// here I will like to cast q to an array of items of type table!