有人可以根据性能,内存使用,编码的简易性,正确的事情等因素,在使用语句中包含DataContext或在LINQ-SQL中包含Data/Text之间的优缺点.
更新:在一个特定的应用程序中,我经历过,没有将DataContext包装在使用块中,随着活动对象未针对GC发布,内存使用量不断增加.在下面的示例中,如果我持有对q的列表和q的访问实体的引用,我创建了一个未针对GC发布的对象图.
使用DataContext
using (DBDataContext db = new DBDataContext())
{
var q =
from x in db.Tables
where x.Id == someId
select x;
return q.toList();
}
Run Code Online (Sandbox Code Playgroud)
DataContext没有使用和保持活着
DBDataContext db = new DBDataContext()
var q =
from x in db.Tables
where x.Id == someId
select x;
return q.toList();
Run Code Online (Sandbox Code Playgroud)
谢谢.
策略模式和委托模式(不是委托)之间有什么区别?
我将t-sql db中的日期时间保存为UTC而不是本地时间,即保存后,它会丢失它是UTC日期ex : 2011-11-08 00:00:00.000
. 在C#中从db读取时,它被读取为本地时间而不是UTC.例如:在读取dateTime值后,a dateTime.ToUniversalTime()
给出了不同的值.
如何将db datetime值读取为UTC而不是本地时间?或者我应该在当地时间保存在t-sql中?
在Git中撤消更改(分阶段和非分阶段)的最快方法是什么?
两个文件都未分级.
$ git status -s
M file1.txt # unstaged
?? oops.txt # unstaged
Run Code Online (Sandbox Code Playgroud)
一个文件暂存,一个文件未分级.
$ git status -s
M file1.txt # staged
?? oops.txt # unstaged
Run Code Online (Sandbox Code Playgroud)
我可以add
全部索引,然后stash
save
和drop
.
$ git add .
$ git stash save
$ git stash drop
$ git status
nothing to commit, working directory clean
Run Code Online (Sandbox Code Playgroud)
有更快的方法吗?
在SQL查询中检查列的空值或值的有效方法是什么.考虑一个table
带有column
索引的整数列的sql表.@value
可以是一些整数或null ex:16或null.
查询1:不确定,但似乎不应该依赖SQL中的短路.但是,当@value
某个整数或null 时,以下查询始终可以正常工作.
select * from
table
where (@value is null or column = @value)
Run Code Online (Sandbox Code Playgroud)
以下查询是上述查询的扩展版本.它也正常工作.
select * from
table
where ((@value is null)
or (@value is not null and column = @value))
Run Code Online (Sandbox Code Playgroud)
以上2个查询是否会利用索引?
查询2:下面的查询将列与非null @value
进行比较,否则将列column
与其自身进行比较,该列始终为true并返回所有内容.它也正常工作.这个查询会利用索引吗?
select * from
table
where (column = isnull(@value, column))
Run Code Online (Sandbox Code Playgroud)
什么是最好的方式?
注意:如果答案因数据库而异,我对MS-SQL感兴趣.
我怎样才能将这两种方法都编译好?
public static IEnumerable<string> DoSomething(params string[] args)
{ // do something }
public static IEnumerable<string> DoSomething(this string[] args)
{ // do something }
Run Code Online (Sandbox Code Playgroud)
我得到这个编译错误:
Type 'Extensions' already defines a member called 'DoSomething' with the same parameter types Extensions.cs
Run Code Online (Sandbox Code Playgroud)
所以我可以这样做:
new string[] { "", "" }.DoSomething();
Extensions.DoSomething("", "");
Run Code Online (Sandbox Code Playgroud)
如果没有params方法,我必须这样做:
Extensions.DoSomething(new string[] { "", "" });
Run Code Online (Sandbox Code Playgroud)
更新:根据OR Mapper的答案
public static IEnumerable<string> DoSomething(string arg, params string[] args)
{
// args null check is not required
string[] argscopy = new string[args.Length …
Run Code Online (Sandbox Code Playgroud) 我需要以编程方式下载大文件,然后再进行处理.最好的方法是什么?由于文件很大,我想要特定的时间等待,以便我可以强行退出.
我知道WebClient.DownloadFile().但似乎没有办法确定等待一段时间以便强行退出.
try
{
WebClient client = new WebClient();
Uri uri = new Uri(inputFileUrl);
client.DownloadFile(uri, outputFile);
}
catch (Exception ex)
{
throw;
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用命令行实用程序(wget)下载文件并使用ProcessStartInfo触发命令并使用Process'WellForExit(int ms)强制退出.
ProcessStartInfo startInfo = new ProcessStartInfo();
//set startInfo object
try
{
using (Process exeProcess = Process.Start(startInfo))
{
//wait for time specified
exeProcess.WaitForExit(1000 * 60 * 60);//wait till 1m
//check if process has exited
if (!exeProcess.HasExited)
{
//kill process and throw ex
exeProcess.Kill();
throw new ApplicationException("Downloading timed out");
}
}
}
catch (Exception ex)
{
throw;
} …
Run Code Online (Sandbox Code Playgroud) 我正在解析一个大的xml文件.所以我使用XmlReader结合XElement而不是XElement.Load().
我已经从XmlReader中的XElement对象创建的,如下所示,并在这里.
static IEnumerable<XElement> StreamRootChildDoc(string uri)
{
using (XmlReader reader = XmlReader.Create(uri, xmlReaderSettings))
{
reader.MoveToContent();
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name == "Child")
{
XElement el = XElement.ReadFrom(reader) as XElement;
if (el != null)
yield return el;
}
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想将此XElement对象内容作为字符串保存在数据库中而不使用空格.以下3种方式都不起作用.请注意,如果我使用XElement.Load()在内存中加载xml,则ToString(SaveOptions.DisableFormatting)可以正常工作.
<root> <child></child> </root> //xml saved in db with whitespace
<root><child></child></root> //want to save as this
XElement.ToString(SaveOptions.DisableFormatting) //
XElement.ToString(SaveOptions.None)
XElement.ToString()
Run Code Online (Sandbox Code Playgroud)
我用于XmlReader对象的XmlReaderSettings如下所示.我尝试IgnoreWhitespace …
我想从 json 中删除尾随逗号,
{
"key1": "value1",
"object": {
"key2": "value2", // <- remove comma
},
"key3": "value3", // <- remove comma
}
Run Code Online (Sandbox Code Playgroud)
我想出了,
tr -d '\n' | \
sed -E 's:,(\s*}):\1:g' | \
jq .
Run Code Online (Sandbox Code Playgroud)
它有效,但我想把它完全纳入sed
.
我想出了,
sed -E '/,\s*$/ { N; s:,\s*(\n\s*},?):\1: }'
Run Code Online (Sandbox Code Playgroud)
这适用于上述输入,但失败
{
"key1": "value1",
"object": {
"key2": "value2",
},
"key3": "value3",
"key4": "value4", // <- remove comma
}
Run Code Online (Sandbox Code Playgroud)
asN
读取下一行并从下一行开始。
// output sed -E '/,\s*$/ { N;l }' using l/look command
{
"key1": …
Run Code Online (Sandbox Code Playgroud) 我想读一个大的xml文件(100 + M).由于它的大小,我不想使用XElement将其加载到内存中.我正在使用linq-xml查询来解析和读取它.
最好的方法是什么?关于XPath或XmlReader与linq-xml/XElement组合的任何示例?
请帮忙.谢谢.
c# ×3
large-files ×2
t-sql ×2
xelement ×2
bash ×1
comparison ×1
datacontext ×1
datetime ×1
delegation ×1
download ×1
git ×1
json ×1
linq ×1
linq-to-sql ×1
linq-to-xml ×1
null ×1
params ×1
sed ×1
sql-server ×1
using ×1
utc ×1
webclient ×1
whitespace ×1
xml ×1
xmlreader ×1
xpath ×1