Question

I'm using MVCMiniProfiler to profile some database queries. For one query it fails to show the Where part of the query that I have defined in my code.

code is as follows:

MyAppDataContext.cs:

public partial class MyAppDataContext : System.Data.Linq.DataContext {
    public static MyAppDataContext CreateNewContext() {

        var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyAppConnectionString"].ToString());
        var profiledConnection = new ProfiledDbConnection(sqlConnection, MiniProfiler.Current);
        return new MyAppDataContext(profiledConnection);

    }
}

Program code:

MyAppDataContext DataContext = MyAppDataContext.CreateNewContext();
IEnumerable<Requests> Entities = DataContext.Requests;

using (profiler.Step("get data")) {
    var dataset = Entities.Where(x => x.ControleStatus == 4).OrderBy(x => x.ID)
}

I've also tried:

using (profiler.Step("get data")) {
    var dataset = from x in Entities 
                  where x.ControleStatus == 4
                  orderby x.ID
                  select x;
}

This is what MVC Mini Profiler shows me:

SELECT [t0].[ID], [t0].[DatumOntvangst], [t0].[DatumRapport], [t0].[FK_SID], 
    [t0].[ControleStatus], [t0].[SStatus] FROM [dbo].[Request] AS [t0] 

As you can see, there's no Where statement. Btw: The output contains the right results, but as this query takes 30 seconds to complete I'm wondering if all the data is first retrieved from the database and then the where condition is applied in Code (as opposed to on the database).

Was it helpful?

Solution

You need to make it return IQueryable. By returnning IEnumerable, you are not allowing the rest of the expression tree to be converted by the IQueryable provider. The LINQ to Objects versions of the query operators work on IEnumerable. LINQ to SQL requires the IQueryable version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top