views:

601

answers:

5

I'm trying to start using LINQ and specifically LINQ to SQL but I'm having some difficulties

I've tried this with SqlMetal and now using the database table designer in Visual Studio and I keep getting similar errors, like in this code, using the data context I created with the database layout designer in VS2008.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        string usn = UserNameBox.Text;
        string pss = PassBox.Text;

        if (usn == "" || pss == "")
            return;

        DataClassesDataContext dc = new DataClassesDataContext();
        var user = from u in User
                   where u.UserName == usn
                   select u;

    }
}
}

I get an error on the where saying: Could not find an implementation of the query pattern for source type 'System.Security.Principal.IPrincipal'. And also: 'Where' not found.

I had something similar to this when I tried to use the results of SqlMetal. I deleted that source and started over using the designer. I must be missing something here but I can't figure out what. Shouldn't the tables implement what I need since I'm using LINQ to SQL, or do I need to do something extra to make that happen?

+2  A: 

Shouldn't you use the dc you create somewhere in your query?

Something like:

protected void Page_Load(object sender, EventArgs e) 
{ 
  if (Page.IsPostBack) 
  {
    string usn = UserNameBox.Text; 
    string pss = PassBox.Text; 
    if (usn == "" || pss == "")            
      return; 
    DataClassesDataContext dc = new DataClassesDataContext(); 
    var user = from u in dc.User where u.UserName == usn select u; 
  } 
}
Sam
Good point. Umm, I tried to follow the examples I found on the web, but I must be missing where I put in the data context in the var.
Tony Peterson
+10  A: 

Try changing User to dc.User:

var user = from u in dc.User
                   where u.UserName == usn
                   select u;

The User is the property System.Web.UI.Page.User.

smink
+6  A: 

Try

var user = from u in dc.User
                   where u.UserName == usn
                   select u;
Joel Cunningham
That worked. For some reason I didn't think about referencing the dc in there. Thanks.
Tony Peterson
+2  A: 

Your query expression should use dc.User, not just User:

DataClassesDataContext dc = new DataClassesDataContext();
var user = from u in dc.User
           where u.UserName == usn
           select u;

If you've got pluralisation turned on, it may be dc.Users instead.

Jon Skeet
A: 

The User you are referring to (when you don't use dc.User) is the Page.User member. That's why intellisense isn't complaining.

Keltex