小编Chr*_*ris的帖子

如何将字典的内容写入文本文件?

我有一个Dictionary类型的字典对象

并尝试使用StreamWriter将整个内容输出到文本文件,但无法从Dictionary类中找到正确的方法.

using (StreamWriter sw = new StreamWriter("myfile.txt"))
        {
            sw.WriteLine(dictionary.First());

        }
Run Code Online (Sandbox Code Playgroud)

我只能检索第一个元素,它还有一个方括号和中间的逗号分隔符:

[彼得,管理员]

并且很高兴[Peter Admin](没有逗号)

c# linq dictionary

23
推荐指数
2
解决办法
5万
查看次数

如何在C#中动态更改对象的类?

假设我有一个名为Visitor的基类,它有2个子类Subscriber和NonSubscriber.

首先,访问者从NonSubscriber开始,即

NonSubscriber mary = new NonSubscriber();
Run Code Online (Sandbox Code Playgroud)

然后在这个"mary"订阅了一些服务,我想将"mary"的类型更改为Subscriber.

这样做的常规方法是什么?

c#

6
推荐指数
1
解决办法
6312
查看次数

[C#]如何在LINQ to SQL中引入重试逻辑来处理超时?

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到达后抛出一个异常会很好,但我不知道如何以及在哪里适当地抛出它们:-)

任何帮助赞赏.

c# linq linq-to-sql

5
推荐指数
1
解决办法
1343
查看次数

使用未分配的局部变量“字典”

尽管我在以下代码中分配了值,但我仍然收到未分配的局部变量“字典”的使用错误:

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)

c# dictionary

3
推荐指数
1
解决办法
1961
查看次数

如何创建一个返回该类实例集合的类的构造函数?

我的程序有以下类定义:

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.

c# collections constructor linq-to-sql

0
推荐指数
1
解决办法
422
查看次数

标签 统计

c# ×5

dictionary ×2

linq ×2

linq-to-sql ×2

collections ×1

constructor ×1