views:

571

answers:

1

I thought that compiled queries would perform the same query translation as DataContext. Yet I'm getting a run-time error when I try to use a query with a .Contains method call. Where have I gone wrong?

//private member which holds a compiled query.
Func<DataAccess.DataClasses1DataContext, List<int>, List<DataAccess.TestRecord>>
  compiledFiftyRecordQuery = System.Data.Linq.CompiledQuery.Compile
  <DataAccess.DataClasses1DataContext, List<int>, List<DataAccess.TestRecord>>
  ((dc, ids) => dc.TestRecords.Where(tr => ids.Contains(tr.ID)).ToList());

//this method calls the compiled query.
public void FiftyRecordCompiledQueryByID()
{
  List<int> IDs = GetRandomInts(50);

  //System.NotSupportedException
  //{"Parameters cannot be sequences."}

  List<DataAccess.TestRecord> results = compiledFiftyRecordQuery
    (myContext, IDs);         
}
+2  A: 

This article has your answer:

Queries with list parameters cannot be precompiled because the translation of the query is dependent on the number of items in the list.