CompiledQuery in LinqPad

I have seen this example here: It says the compiled query can be and pasted into LinqPad in the article but that is not the case. The query uses a variable "TypedDataContext" which LinqPad does not recognise. I am using LinqPad 5. What is it that I am missing here. I politely suggest that the article is incorrect and you just cant copy and paste that code into LinqPad - you need to get the TypedDataContext from somewhere. Where?

I am trying to work out how to compile a query. Our simplified query I want to compile is:

var flows = FlowModels.Include(x => x.Owner)
.Include(x => x.Status)
.Where(x => x.WorkflowStatusId == 3)

How do I compiled this in LinqPad?

Many thanks.


  • Btw if I call Util.CurrentDataContext, that just returns null.

    My language selection in LinqPad is "C# Statements".
  • I saw an example in another post ...

    "int batchId = 10190;

    var query = CompiledQuery
    .Compile( (UserQuery db, bool isAuditBatch) =>
    x => x.AuditBatchId == batchId && isAuditBatch
    || x.TransferBatchId == batchId && !isAuditBatch));

    // isAuditBatch == true
    query.Invoke(this, true).Dump();

    // isAuditBatch == false
    query.Invoke(this, false).Dump();"

    But once again, the person who posted that does not explain where all the variables come from. What is "UserQuery"?

    I tried this ...

    int statusId = 3;

    var query = System.Data.Linq.CompiledQuery
    .Compile((UserQuery db) =>
    .Where(x => x.WorkflowStatusId == statusId));


    But it complains ... "The type UserQuery cannot be used as type parameter ... There is no implicit reference conversion from UserQuery to System.Data.Linq.DataContect" .

    Everyone, including the doco, is not explaining something to me perhaps? or perhaps I am stupid???
  • edited November 2016
    I also tried this ...

    "var cc = System.Data.Linq.CompiledQuery.Compile((TypedDataContext dc) =>
    from fm in FlowModels
    where fm.WorkflowStatusId == 3
    select fm

    But it just returns ... "CS0246 The type or namespace name 'TypedDataContext' could not be found (press F4 to add a using directive or assembly reference)"

    And I tried ...

    var cc = System.Data.Linq.CompiledQuery.Compile((DataContext dc) =>
    from fm in FlowModels
    where fm.WorkflowStatusId == 3
    select fm

    cc(this).Dump("Status is completed");"

    But "this" = "UserQuery" so no how do I get the DataContext? So getting closer perhaps??

    I also tried this:

    "var ctx = this.GetType().BaseType.Dump();

    var cc = System.Data.Linq.CompiledQuery.Compile((DataContext dc) =>
    from fm in FlowModels
    where fm.WorkflowStatusId == 3
    select fm

    cc(ctx).Dump("Status is completed");"

    But now get "cannot convert from System.Type to system.Data.Linq.DataContext".
  • And note - when I open up your sample Query "Extra - Compiled Query". It also has TypedDataContext in there and cannot find it ....

    "// LINQ to SQL lets you precompile queries so that you pay the cost of translating
    // the query from LINQ into SQL only once. In LINQPad the typed DataContext is
    // called TypeDataContext, so we proceed as follows:

    var cc = CompiledQuery.Compile ((TypedDataContext dc, decimal minPrice) =>
    from c in Customers
    where c.Purchases.Any (p => p.Price > minPrice)
    select c

    cc (this, 100).Dump ("Customers who spend more than $100");
    cc (this, 1000).Dump ("Customers who spend more than $1000");"
  • Compiled queries is for LINQ to SQL. Are you using Entity Framework? If so, this will not work. With EF5 and later, queries are automatically compiled in subsequent executions, so you won't need this anyway.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!