Reputation: 321
Question.
How do I use LINQPad scripts in Visual Studio?
Description.
I created the Class Service.linq
class in LINQPad
.
How can I use the Class Service.linq
class in Visual Studio
?
In other words, can I implement the following scenarios?
Scenarios:
ClassService.linq
to the project in Visual Studio
;ClassService.linq
with the project in Visual Studio
;(synchronization logic:
ClassService.linq
->> changed project in Visual Studio
;ClassService. linq
< < - changed the project in Visual Studio
;)Can I implement all or part of these actions?
Or is LINQPad
used for preliminary development of solutions, and then the code is manually transferred to the *.cs
files?
Is used
Upvotes: 1
Views: 1315
Reputation: 27001
The opposite way is possible: LinqPad 6 now allows to share *.cs
files. Instead of trying to open a .linq file in Visual Studio, you can write your class and store it as .cs file.
This has the following benefits:
To do this, create a new "C# Program" in LinqPad.
Suppose you have a file KeyValueClass.cs
:
using System;
namespace myRedisDemo
{
/// <summary>
/// Class for key / value pair
/// </summary>
/// <example>{"Key":"name", "Value":"Matt"}</example>
public class KeyCalueClass
{
/// <summary>
/// The key
/// </summary>
public string Key { get; set; }
/// <summary>
/// The value
/// </summary>
public string Value { get; set; }
} // class
} // namespace
You want to use it in ReferenceCsFiles.linq
in the
directory C:\Users\Matt\source\repos\A. Demos\myRedisDemo\
as follows:
#load "C:\Users\Matt\source\repos\A. Demos\myRedisDemo\KeyCalueClass.cs"
void Main()
{
var obj = new myRedisDemo.KeyCalueClass();
obj.Dump();
}
This will compile the KeyValueClass.cs
file from your Visual Studio project and run it in LinqPad.
You can even import all .cs files with wildcards like
#load "C:\Users\Matt\source\repos\A. Demos\myRedisDemo\*.cs"
and with all subdirectories like:
#load "C:\Users\Matt\source\repos\A. Demos\myRedisDemo\*.cs /s"
Finally, if you omit the path like
#load "KeyCalueClass.cs"
then it will refer to the same path where the .linq file is. So you could save the supporting *.linq file in the same directory with your classes and just reference the *.cs file(s) without a path.
Upvotes: 1
Reputation: 30934
You can't easily share source code in this manner because the .linq format has an XML header with the query properties (rather like the info in a Visual Studio project file).
However, you can share DLLs by referencing DLLs that you create in VS from LINQPad. LINQPad will automatically locate the .deps.json file if present, and integrate your project's NuGet references into the query. You can access the assembly's internal types from LINQPad if you add the following to a source code file in your project:
[assembly: InternalsVisibleTo("LINQPadQuery")]
To share a DLL, do the following:
Press F4 from LINQPad, click 'Add/Browse', choose a DLL that you created in VS (from the bin\debug folder). That folder will also contain a .deps.json file created by VS which lists all your project's NuGet dependencies. LINQPad will automatically read this file and add those NuGet packages to your query. There's an InternalsVisibleTo example here:
danclarke.com/linqpad-tips-and-tricks-part2
Upvotes: 4