Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

linq - Is there a LINQPad equivalent to a DataContext class?

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...