无法将类型'System.Linq.IQueryable隐式转换为System.Data.Entity.DbSet

时间:2012-10-08 19:45:55

标签: linq

我收到以下错误:

  

无法隐式转换类型   'System.Linq.IQueryable'来   'System.Data.Entity.DbSet'。
明确的转换   存在(你是否错过演员表?)

我有以下代码:

var reports = dbContext.Product;

if (searchField == "StoreNum")
{
    int storeNum;
    int? stnum = int.TryParse(searchString, out storeNum) ? storeNum : (int?)null;

    reports = dbContext.Product
        .Where(w => w.StoreNum == stnum); // error occurs here when I try to build
}

我无法理解为什么我收到这条消息。

2 个答案:

答案 0 :(得分:1)

我认为你需要

List<ProdVal> reports;

if (searchField == "StoreNum")
    {
        int storeNum;
        int? stnum = int.TryParse(searchString, out storeNum) ? storeNum : (int?)null;

       reports = (dbContext.Product
                 .Where(w => w.StoreNum == stnum)).ToList(); // error occurs here when I try to build

    }

答案 1 :(得分:0)

 var reports = dbContext.Product.ToList();

        if (searchField == "StoreNum")
        {
            int storeNum;
            int? stnum = int.TryParse(searchString, out storeNum) ? storeNum : (int?)null;

           reports = reports
          .Where(w => w.StoreNum == stnum).AsQueryable(); // error occurs here when I try to build

        }