我只是在这里画一个空白。

我在“ purchases = Purchases.Where”行上得到的错误是:

Cannot implicitly convert type 'System.Linq.IQueryable<WWLTracker.Models.StockPurchase>' to 'System.Data.Entity.DbSet<WWLTracker.Models.StockPurchase>'. An explicit conversion exists (are you missing a cast?)

我的域模型/类是:

public class StockPurchase
{
    public int StockPurchaseId { get; set; }
    [Required]
    [Display(Name = "Date of purchase")]
    [DataType(DataType.Date)]
    public DateTime Date { get; set; }
    [Required]
    public double Amount { get; set; }

}


控制器:

    // GET: /Popular/
    public ActionResult Index(DateTime? From, DateTime? To)
    {
        var purchases = db.StockPurchases;

        if (From != null && To != null)
        {
            purchases = purchases.Where(x => x.Date >= From && x.Date <= To);
        }
        Return View(purchases.ToList());
    }


我知道这很简单-有人可以告诉我我所缺少的吗?

谢谢,

标记

最佳答案

purchases的类型是从属性db.StockPurchases(即DbSet)推断出来的。 (因此purchases的类型将变为DbSet<WWLTracker.Models.StockPurchase>。您不能稍后将其转换为IQueryable<WWLTracker.Models.StockPurchase>

您应该检查两个结果之间的共享类型为System.Linq.IQueryable<WWLTracker.Models.StockPurchase>,并将purchases的类型设置为该类型。

尝试这个:

IQueryable<WWLTracker.Models.StockPurchase> purchases = db.StockPurchases;

...

10-06 06:11