Dynamic Include statements for eager loading in a query - EF 4.3.1

前端 未结 4 704
我在风中等你
我在风中等你 2020-12-09 11:01

I have this method:

public CampaignCreative GetCampaignCreativeById(int id)
        {
            using (var db = GetContext())
            {
                        


        
4条回答
  •  感动是毒
    2020-12-09 11:35

    Giving the compiler a hint by using IQueryable instead of var will work too.

    IQueryable query = db.CampaignCreatives;
    // or
    DbQuery query = db.CampaignCreatives;
    

    When using var the compiler infers DbSet for query which is more specific than the type returned by Include (which is DbQuery (=base class of DbSet) implementing IQueryable), so you can't assign the result to the query variable anymore. Hence the compiler error on the query = query.Include(include) line.

提交回复
热议问题