0

I am coding a MVC 5 internet application, and have a question in regards to using IQueryable.

If I retrieve a set of DbSet objects as an IQueryable, and I do not reference some of the properties in each DbSet object, are the values for these properties retrieved when using an IQueryable? Is this the advantage of using IQueryable as opposed to IEnumerable?

I am trying to determine the most efficient way of retrieving values from many DbSet objects where I am not referencing each property in each DbSet object.

Thanks in advance.

EDIT

How about in this situation:

If I have a model as follows:

public class TestModel
{
    [Key]
    public long Id { get; set; }
    public string name { get; set; }
    public string value { get; set; }
}

And I retrieve a DbSet of the above object as follows:

IQueryable<TestModel> testModels = await db.testModels(t => t.name.Equals("Test"));

And I iterate through this above IQueryable, retrieving the name property, are the Id and value values ever retrieved from the database?

EDIT2

How about in this situation:

If I retrieve some TestModels as an IQueryable, and then iterate through them, then call the exact same code where the IQueryable has the same name, are two transactions made to the database?

Simon
  • 7,991
  • 21
  • 83
  • 163
  • IQueryable will not fetch data unless you traverse it. If you call Context.SomeTables.ToList() then it will fetch all the records from the database. If you get results in IQueryable and close the database context then you will not be able to fetch data, – fhnaseer Mar 31 '15 at 11:09
  • Just a suggestion, but it would be best not to use the word *attribute* to describe a *property* as these are two different things in C#. – MotoSV Mar 31 '15 at 14:24
  • `ToListAsync` returns a `Task>` not a `Task>`. The code you say you have won't compile. – Servy Mar 31 '15 at 14:32
  • I suggest you look into something like SQL Profiler - it shows all of the queries that is made to your database where stuff like this can be easily verified. – default Mar 31 '15 at 14:43

1 Answers1

0

If you perform a request against the database then it will retrieve all the properties for the specified object. In this instance all of the properties on TestModel will be populated. The benefit of using IQueryable over IEnumerable is you can use the LINQ extension methods, e.g. Where, Select, etc, and you can also use the DbSet within a LINQ statement, i.e. :

from x in dbSetVariable where id == 1 select x

One thing you could try, for example, is:

from x in DbSetVariable select new { newName = x.name }

This will create an anonymous object with a single property called newName and it will be populated with the data from the name column. This may result in a query that requests a single column. If you wanted to request other, individual columns, then simply add new properties to the anonymous type:

from x in DbSetVariable select new { newName = x.name, newValue = x.value }
Ali
  • 3,373
  • 5
  • 42
  • 54
MotoSV
  • 2,348
  • 17
  • 27
  • If you reuse the same `DbContext`, then you may find a second request isn't made to the database, but instead you will get back a cached instance of the object. If you use a new instance, then a new request will be sent to the database. If you use the SQL Server Profiler tool this will show you all requests that are made to the database. Start the profiler, run your application and you will see all the requests made by your application to the database. – MotoSV Mar 31 '15 at 14:54