我不想使用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) 我想创建一个简单的存储库,下面是我使用的方法:
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) 仅使用公共财产是否正确,还是应该建立一个私人领域_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)