【发布时间】:2019-12-29 21:34:35
【问题描述】:
我是 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; }
【问题讨论】:
标签: c# variable-types dynamictype