我有一个Dictionary类型的字典对象
并尝试使用StreamWriter将整个内容输出到文本文件,但无法从Dictionary类中找到正确的方法.
using (StreamWriter sw = new StreamWriter("myfile.txt"))
{
sw.WriteLine(dictionary.First());
}
Run Code Online (Sandbox Code Playgroud)
我只能检索第一个元素,它还有一个方括号和中间的逗号分隔符:
[彼得,管理员]
并且很高兴[Peter Admin](没有逗号)
假设我有一个名为Visitor的基类,它有2个子类Subscriber和NonSubscriber.
首先,访问者从NonSubscriber开始,即
NonSubscriber mary = new NonSubscriber();
Run Code Online (Sandbox Code Playgroud)
然后在这个"mary"订阅了一些服务,我想将"mary"的类型更改为Subscriber.
这样做的常规方法是什么?
LINQ to SQL用于在我的代码中调用一些sprocs,我需要找到在我的数据库调用中添加重试机制的方法...
using (MyDataContext dc = new MyDataContext())
{
int result = -1; //denote failure
int count = 0;
while ((result < 0) && (count < MAX_RETRIES))
{
result = dc.myStoredProc1(...);
count++;
}
result = -1;
count = 0;
while ((result < 0) && (count < MAX_RETRIES))
{
result = dc.myStoredProc2(...);
count++;
}
...
...
}
Run Code Online (Sandbox Code Playgroud)
不确定上面的代码是否正确或是否有任何复杂性.
MAX_RETRIES到达后抛出一个异常会很好,但我不知道如何以及在哪里适当地抛出它们:-)
任何帮助赞赏.
尽管我在以下代码中分配了值,但我仍然收到未分配的局部变量“字典”的使用错误:
private static void UpdateJadProperties(Uri jadUri, Uri jarUri, Uri notifierUri)
{
Dictionary<String, String> dictionary;
try
{
String[] jadFileContent;
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(jadUri.AbsolutePath.ToString()))
{
Char[] delimiters = { '\r', '\n' };
jadFileContent = sr.ReadToEnd().Split(delimiters, System.StringSplitOptions.RemoveEmptyEntries);
}
// @@NOTE: Keys contain ": " suffix, values don't!
dictionary = jadFileContent.ToDictionary(x => x.Substring(0, x.IndexOf(':') + 2), x => x.Substring(x.IndexOf(':') + 2));
}
catch …
Run Code Online (Sandbox Code Playgroud) 我的程序有以下类定义:
public sealed class Subscriber
{
private subscription;
public Subscriber(int id)
{
using (DataContext dc = new DataContext())
{
this.subscription = dc._GetSubscription(id).SingleOrDefault();
}
}
}
Run Code Online (Sandbox Code Playgroud)
,哪里
_GetSubscription()
是一个返回值类型 的sprocISingleResult<_GetSubscriptionResult>
说,我有一个类型List<int>
为1000 id
的类型列表,我想创建一个类型的订阅者的集合List<Subscriber>
.
如果不在循环中调用构造函数1000次,我怎么能这样做?
因为我试图避免频繁地打开/关闭DataContext,这可能会给数据库带来压力.
TIA.