Compiled queries and "Parameters cannot be sequences"
Asked Answered
H

1

7

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);         
}
Hagiography answered 1/5, 2009 at 20:59 Comment(0)
L
12

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.

Laflam answered 2/5, 2009 at 0:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.