小编rec*_*ler的帖子

将Dictionary <string,Task <string >>转换为Dictionary <string,string>

我不想使用foreach循环两次,所以我尝试使用LINQ,但遗憾的是这不起作用:

return _etags.ToDictionary(item => item.Key, async item => await item.Value);
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我应该怎么做才能改进我的代码,或者我如何转换Dictionary<string, Task<string>>Dictionary<string, string>

这是我的代码:

private static async Task<Dictionary<string, string>> GetETagsAsync(List<string> feedsLink)
{
    var eTags = new Dictionary<string, string>(feedsLink.Count);
    System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
    stopWatch.Start();
    var _eTags = new Dictionary<string, Task<string>>(feedsLink.Count);
    eTags = new Dictionary<string, string>(feedsLink.Count);
    foreach (string feedLink in feedsLink)
    {
        if (Uri.IsWellFormedUriString(feedLink, UriKind.Absolute))
        {
            _eTags.Add(feedLink, GetETagAsync(feedLink));
        }
        else
        {
            throw new FormatException();
        }
        }
        foreach (KeyValuePair<string, Task<string>> eTag in _eTags)
        {
            eTags.Add(eTag.Key, await …
Run Code Online (Sandbox Code Playgroud)

c# linq dictionary microsoft-metro

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

使用sqlite-net的通用方法

我想创建一个简单的存储库,下面是我使用的方法:

public static List<T> GetAll()
{
    return _dbConnection.Table<T>().ToList();
}
Run Code Online (Sandbox Code Playgroud)

但我遇到了一个错误: 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'SQLite.SQLiteConnection.Table<T>()'

我用SQLite包装纸sqlite-net.

我的班级是通用的:

class FeedRepository<T> : IFeedRepository<T>, IDisposable where T : IFeed
Run Code Online (Sandbox Code Playgroud)

c# sqlite generics

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

在getter中使用if表达式

仅使用公共财产是否正确,还是应该建立一个私人领域_count?我已经阅读了有关该主题的一些信息,但我找不到答案.

public int Count
{
    get
    {
        if (this._feeds.IsValueCreated)
        {
            return this._feeds.Value.Count;
        }
        else
        {
            return this._dbConnection.Table<T>().Count();
        }
    }
}

public FeedRepository(SQLiteConnection dbConnection)
{
    this._dbConnection = dbConnection;
    this._feeds = new Lazy<IList<T>>(() => _dbConnection.Table<T>().ToList());
}
Run Code Online (Sandbox Code Playgroud)

.net c# accessor

2
推荐指数
1
解决办法
1985
查看次数

标签 统计

c# ×3

.net ×1

accessor ×1

dictionary ×1

generics ×1

linq ×1

microsoft-metro ×1

sqlite ×1