Linq to SQL - What am i missing?
I'm a complete newbie to LINQ, so please bare with my stupid questions.
Using Visual Studio I've created a DBML file, and added a table to it from a local database. This created a data context called UserDataContext
. In my ASPX page I can create an instance of the data context using开发者_运维技巧:
UserDataContext db1 = new UserDataContext();
However when I try to query against the table I get to:
var query = from o in db1.
and from there I can't see the table.... the dbml shows the table has public access.
Any ideas what I've missed?
You need to select it afterwards, so with
var query = from o in db1
would be
var query = from o in db1
select o
Do you have using System.Linq;
at the top of your code file? If you don't intellisense won't pick up for LINQ.
UserDataContext db1 = new UserDataContext();
var query = from o in db1
where o.fieldName = "abc"
select p;
If you want to query all tables in the database, or all columns in a table, I think you'll find this article useful.
Also, as zerkms pointed out in a comment, a LINQ query requires a select statement. It might help if you check out the desugared form of the LINQ query - it certainly helped me in avoiding these kinds of mistakes.
I think he's asking more of why he can't see the table in the query
Have you built your solution since you created the Linq to SQL Models, if you haven't done this they won't show up.
Ensure your class is in the same namespace as your .dbml file. If your .dbml is in a subdirectory, it'll inherit that subdirectory as part of the namespace of the .dbml.
精彩评论