使用foreach循环或ForEach LINQ方法之间是否存在任何差异(性能或其他)?
对于上下文,这是我的一个方法的一部分:
foreach (var property in typeof(Person).GetProperties())
{
Validate(property.Name);
}
Run Code Online (Sandbox Code Playgroud)
我也可以使用此代码执行相同的任务:
typeof(Person)
.GetProperties()
.ToList()
.ForEach(property => Validate(property.Name));
Run Code Online (Sandbox Code Playgroud)
什么时候使用循环结构比使用方法链更好?
这是我使用该ForEach方法的另一个例子,但可以轻松使用foreach循环和变量:
// LINQ
PrivateData.Database.Users
.Cast<User>()
.Where(user => user.LoginType == LoginType.WindowsUser)
.Select(user => new { Name = user.Name, Login = user.Login })
.ToList()
.ForEach(result => WriteObject(result));
// Loop
var users = PrivateData.Database.Users
.Cast<User>()
.Where(user => user.LoginType == LoginType.WindowsUser)
.Select(user => new { Name = user.Name, Login = user.Login });
foreach(var user in users)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个包含6个左右项目的解决方案,在调试配置时都可以正常构建.但是,当我尝试在发布模式下构建它时,我得到53个错误,所有人抱怨无法找到DLL.这只是一些消息:
错误:
警告
我该如何解决这个问题?
调试路径保持不变,但每个项目发布路径都已设置为构建到特定文件夹中.
我跟着这个问题,但我似乎无法让这个工作.
(为了测试)我有一个带有2个脚本的powershell模块:variables.ps1和function.ps1以及一个清单mymodule.psd1(这些文件都在同一个目录中)
这是variables.ps1的内容:
$a = 1;
$b = 2;
Run Code Online (Sandbox Code Playgroud)
这是function.ps1的内容
. .\variables.ps1
function myfunction
{
write-host $a
write-host $b
}
Run Code Online (Sandbox Code Playgroud)
当我导入模块并调用myfunction时.这是输出:
C:\> Import-Module .\mymodule.psd1
C:\> myfunction
. : The term '.\variables.ps1' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Jake\mymodule\function.ps.ps1:8 char:4
+ . .\variables.ps1
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : …Run Code Online (Sandbox Code Playgroud) 我正在尝试将DACPAC部署到LocalDB 2012,但它只是没有它:
堆栈跟踪如下:
==================================
Could not deploy package. (Microsoft.SqlServer.Dac)
------------------------------
Program Location:
at Microsoft.SqlServer.Dac.DeployOperation.Microsoft.SqlServer.Dac.IOperation.Run(OperationContext context)
at Microsoft.SqlServer.Dac.OperationExtension.Execute(IOperation operation, Action`2 reportStatus, CancellationToken cancellationToken)
at Microsoft.SqlServer.Dac.DacServices.InternalDeploy(IPackageSource packageSource, Boolean isDacpac, String targetDatabaseName, DacDeployOptions options, CancellationToken cancellationToken)
at Microsoft.SqlServer.Dac.DacServices.Deploy(DacPackage package, String targetDatabaseName, Boolean upgradeExisting, DacDeployOptions options, Nullable`1 cancellationToken)
at Microsoft.SqlServer.Management.Dac.DacWizard.DeployModel.Install()
at Microsoft.SqlServer.Management.Dac.DacWizard.DeployModel.RunAction()
at Microsoft.SqlServer.Management.Dac.DacWizard.ExecuteDacPage.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
===================================
Unable to connect to target server. (Microsoft.Data.Tools.Schema.Sql)
------------------------------
Program Location:
at Microsoft.Data.Tools.Schema.Sql.Deployment.SqlDeploymentEndpointServer.OnInit(ErrorManager errors, String targetDBName)
at Microsoft.Data.Tools.Schema.Sql.Deployment.SqlDeployment..ctor(SqlDeploymentConstructor constructor)
at Microsoft.Data.Tools.Schema.Sql.Deployment.SqlDeploymentConstructor.ConstructServiceImplementation() …Run Code Online (Sandbox Code Playgroud) 我有以下用C#编写的cmdlet,它基本上只是抛出一个错误:
[Cmdlet("Use", "Dummy")]
public class UseDummyCmdlet :PSCmdlet
{
protected override void ProcessRecord()
{
var errorRecord = new ErrorRecord(new Exception("Something Happened"), "SomethingHappened", ErrorCategory.CloseError, null);
ThrowTerminatingError(errorRecord);
}
}
Run Code Online (Sandbox Code Playgroud)
我假设(我可能是错的),这在PowerShell中是等价的)
Function Use-Dummy()
{
[CmdletBinding()]
Param()
process
{
$errorRecord = New-Object System.Management.Automation.ErrorRecord -ArgumentList (New-Object System.Exception), 'SomethingHappened', 'NotSpecified', $null
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
}
Run Code Online (Sandbox Code Playgroud)
PowerShell版本的行为符合预期:
Use-Dummy : Exception of type 'System.Exception' was thrown.
At line:1 char:10
+ use-dummy <<<<
+ CategoryInfo : NotSpecified: (:) [Use-Dummy], Exception
+ FullyQualifiedErrorId : SomethingHappened,Use-Dummy
Run Code Online (Sandbox Code Playgroud)
然而,C#版本崩溃了,包含以下信息:
An exception of type 'System.Management.Automation.PipelineStoppedException' occurred …Run Code Online (Sandbox Code Playgroud) 我有一个 PowerShell 函数,它需要一个字符串和三个开关。所以调用会是这样的:
My-Function "some string" -Switch1 -Switch2 -Switch3
Run Code Online (Sandbox Code Playgroud)
但是,我想让此函数仅在提供了字符串和至少一个开关的情况下运行。所以:
My-Function "some string" -Switch1 -Switch3 # Valid
My-Function "some string" -Switch2 # Valid
My-Function "some string" # Invalid
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过检查是否已使用对象传递开关来实现这一点$MyInvocation,但是有没有办法使用Parameter和ParameterSet属性来做到这一点?
简而言之,这就是我正在尝试做的事情:
注意:这似乎是与SSDT项目一起使用的编译器的一个问题,它显然已在2017年RC中修复.我的问题类似于这里描述的问题.
我有一些代码拒绝让我把它写成一个表达式的函数成员.简而言之,我想这样做:
void foo() => bar();
Run Code Online (Sandbox Code Playgroud)
但IDE发脾气并要求我这样写:
void foo() { bar(); }
Run Code Online (Sandbox Code Playgroud)
我的意思是,这是两个额外的角色,但我不确定它为什么抱怨,错误也没有意义.它给了我以下3个错误:
完整代码看起来像这样.
public static void foo() => bar("some param"); // Errors on this line.
static void bar(string myParam) { //20 lines of code }
Run Code Online (Sandbox Code Playgroud)
我在C#交互式窗口中对此进行了测试,所有内容都编译并正确运行.我在代码中找不到任何不可打印的字符.
这是使用VS 2015社区,目标框架是4.6.1
完整代码:
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
public partial class Triggers
{
private const string ConnectionString = "context connection = true";
private const string ReadInsertedTable = @"
SELECT ID,
(
SELECT *
FROM inserted AS …Run Code Online (Sandbox Code Playgroud) 我正在使用SMO,我有这行代码:
var results = (from User user in database.Users
where user.LoginType == LoginType.WindowsUser
select new { user.Name, user.Login }).ToList();
Run Code Online (Sandbox Code Playgroud)
但无论出于何种原因,我无法像这样编写查询:
var results = database.Users
.Where(user => user.LoginType == LoginType.WindowsUser)
.Select(user => new { Name = user.Name, Login = user.Login })
.ToList();
Run Code Online (Sandbox Code Playgroud)
我得到的错误是 'UserCollection' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'UserCollection' could be found (are you missing a using directive or an assembly reference?)
但据我所知,这两种说法都是一致的.
为什么会这样?
在 SQL Server 中选择数据库角色和应用程序角色时应该考虑哪些事项?
我了解每种角色的工作方式,但我很难理解应用程序角色何时适合用于数据库角色。
到目前为止,我只是在我的应用程序中使用了数据库角色。我基本上为每个用户创建了一个帐户,然后将他们分配给他们需要访问 usingGRANT语句的任何角色。有问题的应用程序只是一个简单的数据输入/报告应用程序。
使用应用程序角色是否有任何好处,如果有,我该如何使用它们?具体来说,我如何确保给定用户无法访问他们不应该访问的对象。我需要多个应用程序角色还是有其他方法?
我觉得我在这里遗漏了一些明显的东西......
我的 WPF 应用程序中有一个位于子文件夹中的文本文件,因此对于糟糕的 ASCII 提前表示歉意。
+Project
+--+Subfolder
| +--TextFile.txt
|
+--App.config
+--App.xaml
+--etc.
Run Code Online (Sandbox Code Playgroud)
此文本文件上的构建操作是 Resource,我正在尝试将内容作为程序中的字符串访问,但我完全不知道我在做什么。
尝试通过Properties.Settings.Default无法访问文件,显然我的程序中只有一个 ConnectionString 资源。
我无法在 XAML 中执行此操作,因为无论出于何种原因都没有Source属性
<!-- somewhere up the top of App.xaml... -->
xmlns:clr="clr-namespace:System;assembly=mscorlib"
<clr:String Source="pack://application:,,,/Subfolder/Textfile.txt"/>
Run Code Online (Sandbox Code Playgroud)
FindResource 方法也找不到它。
FindResource("Usage.txt"); //ResourceReferenceKeyNotFoundException
Run Code Online (Sandbox Code Playgroud)
我要做的就是引用文本文件,将其作为字符串读取并使用该字符串。否则我必须在方法调用中嵌入一个 50 行的逐字字符串。因为这完全是个好主意。/秒
在 WinForms 中,它很简单:Properties.Settings.Default.TextFile.ToString();但这里似乎没有任何效果。
我还应该指出,这个文件不应该包含在输出目录中,它需要嵌入到应用程序或任何术语中。
我该怎么做?
在我的应用程序中,我想从 WCF 服务(作为 Windows 服务托管)返回一组对象,以填充 WPF 应用程序中的 DataGrid。集合中的对象数量从一到几百不等,具体取决于调用的方法。
我很好奇处理从服务返回大型集合的“最佳”方法是什么。
这些是我看到的建议选项:
yield关键字做一些事情,但这超出了我的头脑,我无法遵循它。:-/完成这项任务的最佳方法是什么,为什么?