Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.DbSet'

前端 未结 4 1835
陌清茗
陌清茗 2020-12-03 09:58

I\'m new in Linq and so I have these situation below.

Now below error during compilation, says Cannot implicitly convert type \'System.Linq.IQuery

4条回答
  •  盖世英雄少女心
    2020-12-03 10:39

    When using var the compiler infers the type of the expression to the right of the assignment. When you write

    var query = _db.Products;
    

    query is of type DbSet, and it cannot be assigned any IQueryable, which the Where extension method returns.

    When you used the query syntax, query was again IQueryable, which made it work. It's equivalent to writing

    var query = products.Select(t => t);
    

    The Select extension method, like Where, returns IQueryable.

提交回复
热议问题