137

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?

Thank you.

Schoof
  • 2,715
  • 5
  • 29
  • 41

10 Answers10

329

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();
    // ...
k.m
  • 30,794
  • 10
  • 62
  • 86
  • My DataClasses1.Desinger.cs (code LINQ auto generates) does not include IEnumerable. `public System.Data.Linq.Table tblPersoons { get { return this.GetTable(); } }` And when I use: var query = (from p in tblPersoon.Cast() select p).Single(); It gives me the following errors on .Cast. **'SilverlightApplication1.Web.tblPersoon' does not contain a definition for 'Cast'** – Schoof Nov 21 '11 at 17:48
  • 1
    @ThomasSchoof: could it be typo? Property is called `tblPersoons` (note the **s** at the end) while type is `tblPersoon`. You query type, instead of property on type. – k.m Nov 21 '11 at 17:51
  • If I try `var query = (from p in tblPersoons select p).Single();` It tells me tblPersoons doesn't even excist. – Schoof Nov 21 '11 at 17:53
  • The code for the generated class of my table can be found here: http://pastebin.com/edx3XRhi – Schoof Nov 21 '11 at 18:00
  • You need an instance of type which defines this property. See my edit, just noticed you pasted code. – k.m Nov 21 '11 at 18:01
  • Wow, thank you, that isn't giving any more errors. But I don't recognise that code, how would you go about on inserting a persoon in the database in this way? Thank you so much! :D Would this work? `context.tblPersoons.InsertOnSubmit` – Schoof Nov 21 '11 at 18:08
  • 1
    I don't think LINQ to SQL was meant to update database (hence **query** in Language INtegrated Query), however you can find some help with those topics on [LINQ to SQL MSDN page](http://msdn.microsoft.com/en-us/library/Bb386929(v=VS.90).aspx) (*Updating Without Querying* section). – k.m Nov 21 '11 at 18:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5203/discussion-between-jimmy-keen-and-thomas-schoof) – k.m Nov 21 '11 at 18:46
  • 47
    Adding **using System.Linq** did it for me.. :) – Guruprasad J Rao Sep 28 '15 at 18:12
  • "Remove and Sort Usings" just cost me 10 minutes... but the error totally makes sense when you think about it. – ThomasRones Dec 31 '18 at 05:03
  • 1
    Visual Studio 2019 --> Missing System.Linq causes this, and Intellisense won't necessarilly list it automatically. – Mike Upjohn May 05 '20 at 10:26
264

You may need to add a using statement to the file. The default Silverlight class template doesn't include it:

using System.Linq;
Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
40

Make sure these references are included:

  • System.Data.Linq
  • System.Data.Entity

Then add the using statement

using System.Linq;
MobileMon
  • 8,341
  • 5
  • 56
  • 75
26

You must have forgotten to add a using statement to the file like this:

using System.Linq;
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Alexey Derepa
  • 361
  • 3
  • 3
7

I had a similar issue with generated strongly typed datasets, the full error message was:

Could not find an implementation of the query pattern for source type 'MyApp.InvcHeadDataTable'. 'Where' not found. Consider explicitly specifying the type of the range variable 'row'.

From my code:

        var x =
            from row in ds.InvcHead
            where row.Company == Session.CompanyID
            select row;

So I did as it suggested and explicitly specified the type:

        var x =
            from MyApp.InvcHeadRow row in ds.InvcHead
            where row.Company == Session.CompanyID
            select row;

Which worked a treat.

Stephen Turner
  • 7,125
  • 4
  • 51
  • 68
5

You are missing an equality:

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

where clause must result in a boolean.

OR you should not be using where at all:

var query = (from p in tblPersoon select p).Single();
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • Thank, I was indeed missing my equality, which was stupid of me. But I now I am getting the following error: Error 1 Could not find an implementation of the query pattern for source type 'SilverlightApplication1.Web.tblPersoon'. 'Where' not found. – Schoof Nov 21 '11 at 17:37
1

For those of you (like me) that wasted too much time from this error:

I had received the same error: "Could not find implementation of query Pattern for source type 'DbSet'" but the solution for me was fixing a mistake at the DbContext level.

When I created my context I had this:

public class ContactContext : DbContext
    {
        public ContactContext() : base() { }

        public DbSet Contacts { get; set; }
    }

And my Repository (I was following a Repository pattern in ASP.NET guide) looked like this:

public Contact FindById(int id)
    {       
        var contact = from c in _db.Contacts where c.Id == id select c;
        return contact;
    }

My issue came from the initial setup of my DbContext, when I used DbSet as a generic instead of the type.

I changed public DbSet Contacts { get; set; } to public DbSet<Contact> Contacts { get; set; } and suddenly the query was recognized.


This is probably what k.m says in his answer, but since he mentioned IEnumerable<t> and not DbSet<<YourDomainObject>> I had to dig around in the code for a couple hours to find the line that caused this headache.

TylerSmall19
  • 141
  • 1
  • 4
0

I had the same error as described by title, but for me it was simply installing Microsoft access 12.0 oledb redistributable to use with LinqToExcel.

BanMe
  • 133
  • 1
  • 8
0

Hi easiest way to do this is to convert this IEnumerable into a Queryable

If it is a queryable, then performing queries becomes easy.

Please check this code:

var result = (from s in _ctx.ScannedDatas.AsQueryable()
                              where s.Data == scanData
                              select s.Id).FirstOrDefault();
                return "Match Found";

Make sure you include System.Linq. This way your error will be resolved.

Abhay Shiro
  • 3,431
  • 2
  • 16
  • 26
0

I had the same error, but for me, it was attributed to having a database and a table that were named the same. When I added the ADO .NET Entity Object to my project, it misgenerated what I wanted in my database context file:

// Table
public virtual DbSet<OBJ> OBJs { get; set; }

which should've been:

public virtual DbSet<OBJ> OBJ { get; set; }

And

// Database?
public object OBJ { get; internal set; }

which I actually didn't really need, so I commented it out.

I was trying to pull in my table like this, in my controller, when I got my error:

protected Model1 db = new Model1();

public ActionResult Index()
{
    var obj =
        from p in db.OBJ
        orderby p.OBJ_ID descending
        select p;

    return View(obj);
}

I corrected my database context and all was fine, after that.

vapcguy
  • 7,097
  • 1
  • 56
  • 52