我是 C# 的初学者,在正确理解泛型类型时遇到了一些麻烦。在此示例中,我想以某种方式将查询结果存储到变量中。
我在下面显示的代码不正确,因为应该指定泛型类型 T。
public class Data
{
public IQueryable<T> Results { get; set; }
public Data()
{
var db = new Database();
}
public void Store()
{
Results = db.Products.Select(x => new { x.ProductName, x.Cost });
}
}
是否可以在不声明仅用于一种用途的特殊类的情况下做到这一点,比如这个?
public class ProductView
{
public string ProductName { get; set; }
public int Country { get; set; }
}
...
public IQueryable<ProductView > Results { get; set; }
另外,为什么动态类型不适合这个例子?
public dynamic Results { get; set; }