在SQLite中,如何选择some_column为空的记录?
空计为NULL和"".
我可以做这个:
var log = string.Format("URL: {0}", url);
Run Code Online (Sandbox Code Playgroud)
或者甚至喜欢这个
var format = "URL: {0}";
...
var log = string.Format(format, url);
Run Code Online (Sandbox Code Playgroud)
我在format其他地方定义并使用format变量,而不是内联字符串.
在C#6中,这似乎是不可能的:
var format = $"URL: {url}"; // Error url does not exist
...
var url = "http://google.com";
...
var log = $format; // The way to evaluate string interpolation here
Run Code Online (Sandbox Code Playgroud)
无论如何使用字符串插值与先前声明的变量?
C#6似乎在编译期间内联字符串内联.但是,请考虑使用此功能进行本地化,在config中定义格式或仅const在类中使用格式.
我创建了一个简单的单元测试项目来读取app.config文件.目标框架是Core 2.0.我还创建了一个Core 2.0控制台应用程序,以便对自己进行完整性检查,以确保我没有做任何奇怪的事情(在.NET 4.6.1单元测试项目中,同样的测试通过了预期).
控制台应用程序读取app.config很好,但单元测试方法失败,我无法弄清楚原因.两者都使用相同app.config的副本(未添加为链接),并且都安装了System.Configuration.ConfigurationManager v4.4.1 NuGet包.
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Test1" value ="This is test 1."/>
<add key="Test2" value ="42"/>
<add key="Test3" value ="-42"/>
<add key="Test4" value="true"/>
<add key="Test5" value="false"/>
<add key="Test6" value ="101.101"/>
<add key="Test7" value ="-1.2345"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
单元测试
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
namespace ConfigTest
{
[TestClass]
public class UnitTest1
{
[TestMethod()]
public void ConfigTest()
{
foreach (string s in ConfigurationManager.AppSettings.AllKeys)
{
System.Console.WriteLine(s);
System.Diagnostics.Debug.WriteLine(s);
}
//AllKeys.Length is 0? Should be 7...
Assert.IsTrue(ConfigurationManager.AppSettings.AllKeys.Length == …Run Code Online (Sandbox Code Playgroud) 我想知道什么时候我们应该在windows窗体程序中覆盖OnPaint时调用base.OnPaint?
我在做的是:
private void Form1_Paint(object sender, PaintEventArgs e)
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
base.OnPaint(e);
}
Run Code Online (Sandbox Code Playgroud)
我得到stackoerflowexception,为什么?
我正在使用以下代码来查找基于用户将指定的搜索短语的"描述".将Keys视为重要的短语,将值视为关于在何处查找该短语的描述.
可以把它想象成一个商店定位器(我能想到最好的比喻).如果您搜索"目标"(键),您将获得大量的城市(价值),有些可能具有相同的名称.因此,同一城市可以有多个目标.
A Dictionary显然不起作用,因为我要么有重复的商店名称,要么可能有重复的城市名称.这让我了解了我的实际情况:
基本上,我从a开始List<KeyValuePair<string, string>>,允许在两个方向上重复,然后将其转换为Lookup<string, string>我认为不会比它变得更混乱的那个.
List<KeyValuePair<string, string>> kvpList = new List<KeyValuePair<string, string>>();
Lookup<string, string> collection;
kvpList.Add(new KeyValuePair<string, string>("K1", "R1"));
kvpList.Add(new KeyValuePair<string, string>("K1", "R1"));
kvpList.Add(new KeyValuePair<string, string>("K1", "R2"));
kvpList.Add(new KeyValuePair<string, string>("K2", "R1"));
kvpList.Add(new KeyValuePair<string, string>("K2", "R2"));
kvpList.Add(new KeyValuePair<string, string>("K2", "R3"));
kvpList.Add(new KeyValuePair<string, string>("K2", "R1"));
collection = (Lookup<string,string>)kvpList.ToLookup(k => k.Key, k => k.Value);
Run Code Online (Sandbox Code Playgroud)
以上只是虚假的测试信息,但我觉得必须有一个更清晰的方法来获得Lookup特别的结果,因为它似乎非常Linq友好.不幸的是,我对Linq很不熟悉,Linq本身的语法似乎并不适合初学者友好.我在此代码中得到了结果(硬编码搜索术语仅用于测试目的):
string searchTerm = "K2";
List<string> uniqueResults = new List<string>();
foreach (var item in collection)
{
if (item.Key.Contains(searchTerm))
{
foreach …Run Code Online (Sandbox Code Playgroud) 当我不小心将鼠标悬停在 NumericUpDown 上时,我一直在尝试找到一种方法来禁用 NumericUpDown 的滚动。有没有办法使用属性来做到这一点?
我找到了一种使用组合框代码来禁用它的方法,但不是直接在设计器视图的“属性”面板中禁用它。
这是我找到的代码:
void comboBox1_MouseWheel(object sender, MouseEventArgs e) {
((HandledMouseEventArgs)e).Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
我从这个链接找到了代码:
我的设计中有很多这样的 NumericUpDown,因此希望有一种方法可以在我不小心将鼠标悬停在 NumericUpDown 上时禁用鼠标滚动。因此,如果有一种方法可以直接在属性中执行此操作,将会有很大帮助。提前致谢!
如何删除 Visual Studio Code 中的项目?
我到处点击寻找解决方案。我已经清除了最近打开、重新启动的 VS Code,但该项目没有去任何地方。我在网上查了一下,但有空白。项目似乎永远留在那里!
任何帮助都会很棒。谢谢。
我需要从弹出的JOptionPane(或其他一些弹出窗口)中获取多行输入,但我的大部分搜索都指向那里,这是我被卡住的地方......).我坚持使用
JOptionPane.showInputDialog(null, new JTextArea(20,20));
Run Code Online (Sandbox Code Playgroud)
但我希望只将20x20区域读取到一个字符串,而不显示任何文本字段.我认为必须有一些方法可以做到这一点,但其他类型的对话框似乎只返回一个int ...它不一定是一个JOptionPane,只要它是一个单独的弹出窗口,我的主GUI可以读回一个字符串进行读取.
它exception具体返回line 12.
public void saveToXML()
{
URL newURL = new URL();
newURL.type = type;
newURL.name = name;
newURL.info = info;
newURL.url = url;
newURL.isProtected = isProtected;
newURL.amountOfClicks = amountOfClicks;
XmlSerializer xml = new XmlSerializer(typeof(URL));
string directory = @"C:\Users\PC-User\Documents\Link" + newURL.name + ".xml";
using (var file = File.Create(directory))
{
xml.Serialize(file, url);
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要,在异常消息中提供更多详
不应在UI线程上执行同步操作.考虑在Task.Run中包装此方法.
谢谢!
c# ×7
.net-core ×1
app-config ×1
c#-6.0 ×1
filestream ×1
java ×1
joptionpane ×1
jtextarea ×1
linq ×1
mousewheel ×1
select ×1
sql ×1
sqlite ×1
swing ×1
unit-testing ×1
webview2 ×1
winforms ×1