技术

1,353 次浏览

最佳实践:根据条件获取集合的部分结果

很多场景下,我们需要根据一个条件判定某一集合中是否存在从中选取部分符合条件的元素用于后续操作。我们有很多方式可以实现这种需求,比如 纯手工对集合进行遍历,yield return,Any(),Count() 或 Count 属性,那么,这些实现方式对效率的影响如何?哪种实现效率较优呢?

我们来做一次实验。

首先我们定义一个类,并初始化包含有 1 万个该类型实例的集合。

    public class Item
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public bool Gender { get; set; }
        public int Age { get; set; }
        public DateTime Birthday { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
    }

Continue reading

2,765 次浏览

如何将 XML 的 XSD 定义转换为 C# 类

当我们将一些配置数据存储在 XML 文档中,或希望自定义某种 XML 格式的文档时,最好先去定义 XML 对应的 XSD 规范。定义 XSD 后,我们不仅可以在 VS 等 IDE 中对相应的 XML 文件的语法进行自动检查或属性匹配,同时也可以自动生成对应的实体类,简化 XML 的创建、修改以及读取。

打开 VS,在 XSD 文件上点击鼠标右键,点选 Open Command Prompt

Continue reading

1,379 次浏览

Dynamics 365: Three way to get organization unique name

We have three ways to get organization unique name in Microsoft Dynamics 365.

A. Just click on the top right corner of the user avatar, and right here (org64e4ed31).

B. Get it with PowerShell, At first, you need to download the Dynamics 365 SDK to local, and then Run the PowerShell as administrator, cd to the SDK folder, and run .\RegisterXRMTooling.ps1

Add-PSSnapin Microsoft.Xrm.Tooling.Connector
Add-PSSnapin Microsoft.Xrm.Tooling.PackageDeployment

$Cred = Get-Credential
Get-CrmOrganizations -Credential $Cred -DeploymentRegion NorthAmerica –OnlineType Office365

C. Get it from the settings page of the Dynamics 365

Click Settings -> Customization -> Developer Resources -> Unique Name

3,828 次浏览

Quick Tips: Open CMD here | 在当前位置打开命令行

We can right-click the mouse with Shift key to find the "Open command here" or "Open PowerShell window here" item to open the command line at current location.

But we often need to run the command line as an administrator, so you could register the following value to Windows Register to add a new item "Open command (as Admin) here" to Windows Explorer right-click menu.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\runas]
"Icon"="c:\\windows\\system32\\cmd.exe"
"MUIVerb"="Open command (as Admin) here"
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

[HKEY_CLASSES_ROOT\folder\shell\runas]
"Icon"="c:\\windows\\system32\\cmd.exe"
"MUIVerb"="Open command (as Admin) here"
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\folder\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

Open the Notepad and paste the text of above, save the file as OpenCMDHere.reg, and then double click this file, and click the "Yes" button in the message box.

The meaning of the above code is that we will create a new Windows shell item called "runas" at folder and the empty space in a folder, and set the Icon and propmt text.

HKEY_CLASSES_ROOT\Directory\shell, the context menu when you right-click on a folder
HKEY_CLASSES_ROOT\Directory\background, the context menu when you right-click on the "background" empty space while in a folder 

Then to define the running command of "runas" item:

cmd.exe /s /k pushd \"%V\"

That means to run the cmd.exe at the current working folder, the /k carries out the command specified by the following string so it executes the command pushd %V and since the only argument pushd accepts is a path it follows that %V a variable delivered by explorer that contains the path of the folder right clicked.

See more:
cmd command, https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490880(v%3dtechnet.10)
Pushd command, https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490978(v=technet.10)


我们可以在 Windows 的任意位置,通过 按下 Shift + 鼠标右键,来在当前位置启动 CMD 或 PowerShell。

但是,我们经常需要以管理员身份取运行 CMD,所以我们可以通过加载上述的值到注册表,来为右键菜单添加一个新的选项。将上述值保存到一个记事本中,并保存为 .reg 文件,然后双击运行,在后续提示中都选择 是,即可。

2,760 次浏览

SQL Server 下 K-V 形式存储 1亿条 数据对于查询效率的影响

这是一篇2年多以前带过的实习生 Peflapos 所整理的小文,拿出来缅怀下,也希望他在新的公司一切顺利。


对1亿条 K-V 型记录查询效率的验证

关于 K-V 型存储

1. 为什么考虑 K-V 存储形式?

假设有一个 Request 类如下:

	public class Request
	{
		public Guid RequestId {get; set;};
		public string RequestName {get; set;};
		public string RequestOwner {get; set;};
	}
	

我们可以按照如下的结构把它存入数据库:

Continue reading

2,488 次浏览

JavaScript: 注册粘贴事件,将剪切板中的内容去除HTML标签,并粘贴到光标位置

$("#Subject").bind('paste', function (e) {
        var pasteData = "";
        if (e.originalEvent.clipboardData) {
            pasteData = e.originalEvent.clipboardData.getData('text');
        } else {
            // for IE
            pasteData = window.clipboardData.getData("Text");
        }
        var self = this;

        // 在等待一段时间后,在当前光标位置,粘贴处理后的文本
        setTimeout(function () {
            var selection = document.getSelection();
            var cursorPos = selection.anchorOffset;
            var oldContent = selection.anchorNode.nodeValue;
        // 通过 Jquery的text() 去除所有样式
            var toInsert = $("<div>" + pasteData + "</div>").text();
            var newContent = oldContent.substring(0, cursorPos) + toInsert + oldContent.substring(cursorPos);
            selection.anchorNode.nodeValue = newContent;            
        }, 200);
        return false;
    });

1,513 次浏览
45,743 次浏览

通过 Excel 使用 VBA 计算协方差

作为一名执著的 C# 程序员,当一位学经济的老友从国外找来,让我帮他写完 VBA 作业的时候,一开始,其实我是拒绝的。
各路VB大神,不要嘲笑在下啊,我可是足足用了一个小时才在 Excel 里到了编写 VB 代码的地方 (默认 Office 2013 的那个开发者选项是隐藏的…)
我把一夜写的代码在这里Mark一下,万一以后再有类似的VB任务也不至于抓瞎,哈哈,我是一晚上没睡觉活活憋出来的,以下代码在Excel里计算协方差。
不过话说话来,感觉各种语言都是相同的,学会一种,换成另一种也不会太恐怖。

下载这个 Excel VBA 文件:Spreadsheeting assi
Continue reading