Search code examples
c#sqlsilverlightwcflinq

Could not find an implementation of the query pattern


In my silverlight application I am trying to create a database connection using LINQ. First I add a new LINQ to SQL class, and drag my table called "tblPersoon" into it.

Then in my service file I try to execute the following query:

[OperationContract]
public tblPersoon GetPersoonByID(string id)
{
    var query = (from p in tblPersoon where p.id == id select p).Single();

But at tblPersoon it gives me the following error.

Could not find an implementation of the query pattern for source type 'SilverlightApplication1.Web.tblPersoon'. 'Where' not found.

And even when I try the following:

var query = (from p in tblPersoon select p).Single();

It gives me an error saying 'Select' not found!

Code for the generated class for my table can be found here: http://pastebin.com/edx3XRhi

What is causing this and how would I possibly solve this?


Solution

  • Is the tblPersoon implementing IEnumerable<T>? You may need to do it using:

    var query = (from p in tblPersoon.Cast<Person>() select p).Single();
    

    This kind of error (Could not find an implementation of the query pattern) usually occurs when:

    • You are missing LINQ namespace usage (using System.Linq)
    • Type you are querying does not implement IEnumerable<T>

    Edit:

    Apart from fact you query type (tblPersoon) instead of property tblPersoons, you also need an context instance (class that defines tblPersoons property), like this:

    public tblPersoon GetPersoonByID(string id)
    {
        var context = new DataClasses1DataContext();
        var query = context.tblPersoons.Where(p => p.id == id).Single();
        // ...