typeof (int).Assembly.Dump (1); // Dump just one level deep
typeof (int).Assembly.Dump (7); // Dump 7 levels deep
typeof (int).Assembly.Dump ("mscorlib", 7); // Dump 7 levels deep with heading
GetMyQueries ,取样-返回表示保存的查询/示例的对象集合(例如,使用 Edit | Search All 执行搜索)
突出显示 -包装对象,以便在转储时突出显示黄色
HorizontalRun -允许在同一行上转储一系列对象
LINQPad 还提供 HyperLinq 类,它有两个用途: 第一个用途是显示普通的超链接:
new Hyperlinq ("www.linqpad.net").Dump();
new Hyperlinq ("www.linqpad.net", "Web site").Dump();
new Hyperlinq ("mailto:user@domain.example", "Email").Dump();
你可以把它和 Util.HorizontalRun结合起来:
Util.HorizontalRun (true,
"Check out",
new Hyperlinq ("http://stackoverflow.com", "this site"),
"for answers to programming questions.").Dump();
var xmlFile=Util.CurrentQueryPath.Replace(".linq", ".xml");
var xml = XElement.Load(xmlFile);
var query =
from e in xml.Elements()
where e.Attribute("attr1").Value == "a"
select e;
query.Dump();
此示例返回所有具有属性 attr1的元素,该属性包含 XML 文件中的值 "a",该 XML 文件与查询具有相同的名称并包含在相同的路径中。有关更多代码示例,请查看 这个链接。
var path=@"C:\windows\system32";
var dirSwitch="/s/b";
var x=Util.Cmd(String.Format(@"dir ""{0}"" {1}", path, dirSwitch), true);
var q=from d in x
where d.Contains(".exe") || d.Contains(".dll")
orderby d
select d;
q.Dump();
public class test : ICustomMemberProvider
{
IEnumerable<string> ICustomMemberProvider.GetNames() {
return new List<string>{"Hint", "constMember1", "constMember2", "myprop"};
}
IEnumerable<Type> ICustomMemberProvider.GetTypes()
{
return new List<Type>{typeof(string), typeof(string[]),
typeof(string), typeof(string)};
}
IEnumerable<object> ICustomMemberProvider.GetValues()
{
return new List<object>{
"This class contains custom properties for .Dump()",
new string[]{"A", "B", "C"}, "blabla", abc};
}
public string abc = "Hello1"; // abc is shown as "myprop"
public string xyz = "Hello2"; // xyz is entirely hidden
}
void Main()
{
string inputValue="John Doe";
inputValue=Interaction.InputBox("Enter user name", "Query", inputValue);
if (!string.IsNullOrEmpty(inputValue)) // not cancelled and value entered
{
inputValue.Dump("You have entered;"); // either display it in results window
Interaction.MsgBox(inputValue, MsgBoxStyle.OkOnly, "Result"); // or as MsgBox
}
}
var newP = new Products() { ProductID=pID, CategoryID=cID,
ProductName="Salmon#"+pID.ToString() };
Products.InsertOnSubmit(newP);
SubmitChanges();
更新
var prod=(from p in Products
where p.ProductName.Contains("Salmon")
select p).FirstOrDefault();
prod.ProductName="Trout#"+prod.ProductID.ToString();
SubmitChanges();
删除
var itemsToDelete=Products.Where(p=> p.ProductName.Contains("Salmon") ||
p.ProductName.Contains("Trout"));
foreach(var item in itemsToDelete) { Products.DeleteOnSubmit(item); }
SubmitChanges();
注意: 为了获得前面示例的有效 ID,您可以使用:
var cID = (from c in Categories
where c.CategoryName.Contains("Seafood")
select c).FirstOrDefault().CategoryID;
var pID = Products.Count()+1;
var newP = new Products() { ProductID=pID, CategoryID=cID,
ProductName="Salmon#"+pID.ToString() };
Products.Add(newP);
SaveChanges();
更新
var prod=(from p in Products
where p.ProductName.Contains("Salmon")
select p).FirstOrDefault();
prod.ProductName="Trout#"+prod.ProductID.ToString();
SaveChanges();
删除
var itemsToDelete=Products.Where(p=> p.ProductName.Contains("Salmon") ||
p.ProductName.Contains("Trout"));
foreach(var item in itemsToDelete) { Products.Remove(item); }
SaveChanges();
注意: 为了获得前面示例的有效 ID,您可以使用:
var cID = (from c in Categories
where c.CategoryName.Contains("Seafood")
select c).FirstOrDefault().CategoryID;
var pID = Products.Count()+1;
var prod=(from p in dc.Products
where p.ProductName.Contains("Salmon")
select p).FirstOrDefault();
prod.ProductName="Trout#"+prod.ProductID.ToString();
dc.SaveChanges();
UserQuery dc { get => this; }
void Main()
{
var prod=(from p in dc.Products
where p.ProductName.Contains("Salmon")
select p).FirstOrDefault();
prod.ProductName="Trout#"+prod.ProductID.ToString();
dc.SaveChanges();
}
void Main()
{
var dc=this;
// do the SQL query
var cmd =
"SELECT Orders.OrderID, Orders.CustomerID, Customers.CompanyName,"
+" Customers.Address, Customers.City"
+" FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID";
var results = dc.ExecuteQuery<OrderResult>(cmd);
// just get the cities back, ordered ascending
results.Select(x=>x.City).Distinct().OrderBy(x=>x).Dump();
}
class OrderResult
{ // put here all the fields you're returning from the SELECT
public dynamic OrderID=null;
public dynamic CustomerID=null;
public dynamic CompanyName=null;
public dynamic Address=null;
public dynamic City=null;
}
// needs (F4): System.Windows.dll, System.Windows.Forms.dll,
// WindowsFormsIntegration.dll, PresentationCore.dll, PresentationFramework.dll
void Main()
{
var wfHost1 = new System.Windows.Forms.Integration.WindowsFormsHost();
wfHost1.Height=175; wfHost1.Width=175; wfHost1.Name="Picturebox1";
wfHost1.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
wfHost1.VerticalAlignment=System.Windows.VerticalAlignment.Top;
System.Windows.Forms.PictureBox pBox1 = new System.Windows.Forms.PictureBox();
wfHost1.Child = pBox1;
pBox1.Paint += new System.Windows.Forms.PaintEventHandler(picturebox1_Paint);
PanelManager.StackWpfElement(wfHost1, "Picture");
}
public string pathImg
{
get { return System.IO.Path.Combine(@"C:\Users\Public\Pictures\Sample Pictures\",
"Tulips.jpg"); }
}
// Define other methods and classes here
public void picturebox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// https://stackoverflow.com/a/14143574/1016343
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(pathImg);
System.Drawing.Point ulPoint = new System.Drawing.Point(0, 0);
e.Graphics.DrawImage(bmp, ulPoint.X, ulPoint.Y, 175, 175);
}
这将创建以下图形(面板项目“图形”和“图片”是由上面的例子添加) :
如果你想要 显示 Northwind 数据库中的图像,,你可以做到以下几点:
将图像文件名更改为“ NorthwindPics.jpg”,然后在 第二个例子 Main ()方法的开头添加以下代码:
var img = (from e in this.Employees select e).FirstOrDefault().Photo.ToArray();
using (FileStream fs1 = new FileStream(pathImg, FileMode.Create))
{
const int offset=78;
fs1.Write(img, offset, img.Length-offset);
fs1.Close();
}
void Main()
{
// no code here, but Main() must exist
}
public static class MyExtensions
{
/// <summary>
/// This will list the tables of the connected database
/// </summary>
public static IOrderedEnumerable<string> ListTables(
this System.Data.Linq.DataContext dc, bool dumpIt = true)
{
var query = dc.Mapping.GetTables();
var result = query.Select(t => t.TableName).OrderBy(o => o);
if (dumpIt) result.Dump();
return result;
}
}
Joe (LinqPad 的作者)友好地向我提供了这个片段——它展示了如何将数据上下文传递给 My Extended。 注意: 对于 LinqPad 6或更高版本,您需要按 F4查询属性并勾选“ Reference LINQ-to-SQL Assembly”以使其工作。
void Main()
{
var dc = new DumpContainer().Dump("Some Action");
for (int i = 10; i >= 0; i--)
{
dc.UpdateContent($"Countdown: {i}");
System.Threading.Thread.Sleep(250);
};
dc.UpdateContent("Ready for take off!");
}
显示进度-Util
也可以通过使用如下进度条来显示进展情况:
例如:
void Main()
{
var prog = new Util.ProgressBar("Processing").Dump();
for (int i = 0; i < 101; i++)
{
Thread.Sleep(50); prog.Percent = i;
}
prog.Caption = "Done";
}
[Fact] void TestDb1()
{
var ctx = this; // "this" represents the database context
Assert.True(ctx.Categories.Any(), "No categories");
string.Join(", ", ctx.Categories.Select(s => s.CategoryName).ToList()).Dump("Categories");
Assert.True(ctx.Products.Any(), "No Products");
string.Join(", ", ctx.Products.Select(s => s.ProductName).ToList()).Dump("Products");
}
这个示例需要一个 北风示例数据库(设置为 Linq to SQL 或者 Entity Framework Core Connection) ,分配给查询,并添加 XUnit Test 支持(选择 Query-> Add XUnit Test support in LinqPad)。它将打印所有类别和产品的结果窗口。如果数据库中没有类别或产品,则测试失败。
例二:
// for MemberData
public static IEnumerable<object[]> GetNumbers()
{
yield return new object[] { 5, 1, 3, 9 }; // 1st row: a, b, c, sum (succeeds)
yield return new object[] { 7, 0, 5, 13 }; // 2nd row: a, b, c, sum (fails)
}
[Theory]
[MemberData(nameof(GetNumbers))] // easier to handle than [ClassData(typeof(MyClass))]
void Test_Xunit(int a, int b, int c, int expectedSum)
=> Assert.Equal (expectedSum, a + b + c);
此示例使用带有成员函数 GetNumbers()的 [Theory]来填充参数 a, b, c和 expectedSum,并且比 [Fact]具有更大的灵活性。[Fact]不能有任何参数。如果期望的和不等于 a + b + c 的和,则测试失败。这是第2行的情况: 断言将输出: (a: 7, b: 0, c: 5, expectedSum: 13)。