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.
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:
using System.Linq
)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();
// ...
My DataClasses1.Desinger.cs (code LINQ auto generates) does not include IEnumerable<T>.
public System.Data.Linq.Table<tblPersoon> tblPersoons { get { return this.GetTable<tblPersoon>(); } }
And when I use: var query = (from p in tblPersoon.Cast<Person>() select p).Single(); It gives me the following errors on .Cast. 'SilverlightApplication1.Web.tblPersoon' does not contain a definition for 'Cast'@ThomasSchoof: could it be typo? Property is called
tblPersoons
(note the s at the end) while type istblPersoon
. You query type, instead of property on type.If I try
var query = (from p in tblPersoons select p).Single();
It tells me tblPersoons doesn't even excist.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 (Updating Without Querying section).
Adding using System.Linq did it for me.. :)