Is there a LINQPad equivalent to a DataContext class?
Solution 1
Short answer: You do not need to create the DataContext yourself. LINQPad comes with lots of samples, take a look at them.
When you connect LINQPad to a database, it creates the DataContext for you. The DataContext's tables (Table<T>
) and SubmitChanges()
are available as local members.
For example, LINQPad's default "C# Expression" mode you can just write:
from p in Person
where p.Name == "Joe"
select p.Address
In LINQPad's "C# Statement" mode:
var query = from p in Person
where p.Name == "Joe"
select p.Address;
query.Dump(); // Dump() shows results below
Person joe = query.First();
joe.Name = "Peter";
SubmitChanges();
joe.Dump(); // shows joe's values under the previous query results
LINQPad's Dump()
extension method is very useful can be called on any object or collection (in LINQPad's statement mode) to show the results below.
Note that you don't even need to connect to a database to use LINQPad. You can work with in-memory collections:
int[] numbers = new[] { 1, 2, 3, 4, 5 };
numbers.Where(n => n > 3).Select(n => n * 2).Dump();
In fact, you don't even need to use LINQ to use LINQPad. It also works great as a snippet compiler.
Solution 2
As it was mentioned before, you don't need to create a DataContext as LINQPad creates one by default.
But just in case, you need a second DataContext (for the same database) you could use
var secondDataContext = new UserQuery();
This will create a second datacontext just like the automatically created one.
Solution 3
I know this already has an answer and I agree with Lucas but wanted to add a couple of things that might help readers of this question.
You can load your own DataContext from the assembly if you want to. Regardless of whether you load your own Context or let LinqPad build one for you, you are running in the Context of a "UserQuery" class that is generated by LinqPad.
The following C# statment shows this:
this.GetType().Name.Dump(); // Shows UserQuery
This UserQuery class derives from a DataContext. In the this example I let Linqpad build a datacontext for the AdventureWorks database:
this.GetType().BaseType.Dump(); // Shows TypedDataContext
If I load my own DataContext called MyDataContext:
this.GetType().BaseType.Dump(); // Shows MyDataContext
Solution 4
Building on the reply from jaraics, I found that the UserQuery constructor requires a connection string. At least it does for LINQPad version 4.37.11. Therefore, I retrieved the connection string from the UserQuery instance created by LINQPad and used that to create my own instance.
var connectionString = this.Connection.ConnectionString;
var secondDataContext = new UserQuery(connectionString);
When I used the above code, it gave me a second DataContext.
Solution 5
As per @Thymine 's comment on @DRS9222 's answer, you can simply use something like:
void Main()
{
TypedDataContext db = this;
var qry = from i in db.Items
select i;
// etc. etc.
}
This becomes pretty transferable between LinqPad and your IDE then, rather than have to fixup or remove the context.
Related videos on Youtube
Nick Allen
I have 7 years of experience developing enterprise web applications. For the last 5 years I have been focusing on Sitecore solutions. I regularly attend the Toronto .NET and Sitecore user groups and am an active member and speaker at the Sitecore Toronto user group.
Updated on April 17, 2022Comments
-
Nick Allen about 2 years
I've just begun using LINQPad and so far I like it but most tutorials I have come across for LINQ TO SQL make use of a DataContext class which is generated by Visual Studio for persisting updates etc. I am also fairly new to LINQ TO SQL so my question is what is the equivalent of the following in LINQPad (if there is one)...
MyDbDataContext db = new MyDbDataContext(); ... db.SubmitChanges();
-
Jim Wooley over 12 yearsThe class that your code is generated into is part of the generated DataContext for the selected database. If you need to refer to the context your LINQPad code, use "this". Note: LINQPad doesn't generate the context exactly the same as LINQ to SQL does, so some edge cases may not work the same in LINQPad as they do with LINQ to Sql.
-
-
Jeff LaFay almost 13 yearsWhat about inserting new records with LINQ to SQL and LINQPad? I tried inserting by invoking InsertOnSubmit() on a Table<TEntity> in my database with an identity column and I got an exception "The column cannot be modified". The Id was never specified.
-
James Manning over 11 yearsFWIW, this no longer works (at least with 4.42.01) since the ctor now takes a param of IDbConnection instead, so now you can just do var secondDataContext = new UserQuery(this.Connection);
-
Eric Wu about 8 years@jlafay probably long solved by now, but perhaps your TEntity didn't have a Primary Key setted.
-
Lyubomir Velchev over 7 yearsThanks! That is what is needed to copy and paste queries from visual studio to linqpad with only this small change otherwise have to remove the db context!
-
Thymine over 5 yearsHelped me figure out what I needed to pass in the constructor of a class to be able to run queries in that scope.
public class SubClass { public SubClass(TypedDataContext db) { db.Users.Where(x => x.IsActive); } }
and creating it withnew SubClass(this);
-
CAK2 about 5 yearsUsing LINQPad 5, I applied Andrew's advice as follows: using (var dc = new PulseDataContext(this.Connection.ConnectionString)) { /* your update/insert code here */ dc.SaveChanges(); }