我有以下通用可查询(可能已经应用了选择):
IQueryable<TEntity> queryable = DBSet<TEntity>.AsQueryable();
Run Code Online (Sandbox Code Playgroud)
然后有Provider一个看起来像这样的类:
public class Provider<TEntity>
{
public Expression<Func<TEntity, bool>> Condition { get; set; }
[...]
}
Run Code Online (Sandbox Code Playgroud)
该Condition会每实例用以下方式进行定义:
Condition = entity => entity.Id == 3;
Run Code Online (Sandbox Code Playgroud)
现在我想选择至少由以下一个实体满足的所有Provider实例:ConditionDBSet
List<Provider> providers = [...];
var matchingProviders = providers.Where(provider => queryable.Any(provider.Condition))
Run Code Online (Sandbox Code Playgroud)
这个问题:我正在开始查询Provider列表中的每个实例.我宁愿使用单个查询来获得相同的结果.由于性能可疑,此主题尤其重要.如何使用单个查询获得相同的结果并使用Linq语句提高性能Expression Trees?
我正在使用它PHPhotoLibrary来检索这样的几个照片集:
_smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:nil];
[_smartAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
[self analyzeCollectionInternal:collection];
}];
_userAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];
[_userAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
[self analyzeCollectionInternal:collection];
}];
Run Code Online (Sandbox Code Playgroud)
该方法analyzeCollectionInternal保存以PHAssetCollections供以后使用并显示集合的内容.
- (void)analyzeCollectionInternal:(PHAssetCollection *)collection {
NSLog(@"Album Title %@", collection.localizedTitle);
if (![_collections containsObject:collection]) {
[_collections addObject:collection];
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
另外,我在照片库中注册了一个更改观察者,如下所示:
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
Run Code Online (Sandbox Code Playgroud)
充当Observer的类实现了PHPhotoLibraryChangeObserver这样的协议:
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"PicturesDataSource - photoLibraryDidChange");
NSMutableArray *collectionsToAnalyze = [NSMutableArray new]; …Run Code Online (Sandbox Code Playgroud) 我为我Option的一些项目实现了一种类型,如下所示:
public abstract Option<T> {}
public class None<T> : Option<T>
public class Some<T> : Option<T>
{
public T Value { get; }
public Some(T value)
{
Value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
为了找出一个 Option 是否包含一个值,我使用了这个利用模式匹配的扩展方法:
public static bool TryGetValue<T>(this Option<T> option, out T value)
{
if (option is Some<T> some)
{
value = some.Value;
return true;
}
value = default;
return false;
}
Run Code Online (Sandbox Code Playgroud)
我现在收到以下警告 return default;
无法将空文字转换为不可为空的引用或不受约束的类型参数
我不可能将泛型参数限制T为classor struct。
例如,如果我将泛型参数限制为class,则无法生成Option<int?>实例,因为 …
我目前正在尝试编写AuthenticationMiddleware.看到这个答案.应用程序构建没有错误,但当我执行时,dnx web我收到以下错误:
无法找到"Namespace.BasicAuthenticationMiddleware"类型的合适构造函数.确保类型是具体的,并且构造函数接受所有参数.
在Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider,Type instanceType,Object []参数)
在Microsoft.AspNet.Builder.UseMiddlewareExtensions.<> c__DisplayClass2_0.b__0(RequestDelegate next)
在Microsoft.AspNet.Builder.Internal.ApplicationBuilder.Build()
在Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication()
失败:Microsoft.AspNet.Hosting.Internal.HostingEngine [7]
我确信我使用的构造函数签名在某种程度上是错误的,但我无法为此找到合适的文档,因为似乎有几十个已弃用的文档.
这是AuthenticationMiddleware:
public class BasicAuthenticationMiddleware : AuthenticationMiddleware<BasicAuthOptions>
{
public BasicAuthenticationMiddleware(
RequestDelegate next,
BasicAuthOptions options,
ILoggerFactory loggerFactory,
IUrlEncoder urlEncoder)
: base(next, options, loggerFactory, urlEncoder) {}
protected override AuthenticationHandler<BasicAuthOptions> CreateHandler()
{
return new BasicAuthenticationHandler();
}
}
Run Code Online (Sandbox Code Playgroud)
BasicAuthOptions:
public class BasicAuthOptions : AuthenticationOptions {
public const string Scheme = "BasicAuth";
public BasicAuthOptions()
{
AuthenticationScheme = Scheme;
AutomaticAuthenticate = true;
}
}
Run Code Online (Sandbox Code Playgroud)
BasicAuthenticationExtensions
public static class …Run Code Online (Sandbox Code Playgroud) 我正在使用以下typescript方法来生成UUIDs.代码本身基本上是这个stackoverflow答案的打字稿版本.
generateUUID(): string {
let date = new Date().getTime();
if (window.performance && typeof window.performance.now === 'function') {
date += performance.now();
}
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = (date + Math.random() * 16) % 16 | 0;
date = Math.floor(date / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
};
Run Code Online (Sandbox Code Playgroud)
我们的开发团队TSLint用来保持代码清洁,我们有一个禁止使用的规则bitwise operators.我不知道如何在不损害UUID生成器的加密方面的情况下重写此代码.如何重写这段代码或者这根本没有意义呢?
Exception在C#中创建自定义类型时,为什么重载所有这些构造函数被认为是一种好习惯:
Exception()
Exception(String)
Exception(String, Exception)
Run Code Online (Sandbox Code Playgroud)
请参阅:https: //docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions#include-three-constructors-in-custom-exception-classes
我认为这些建议中没有任何理由.
如果我想创建一个自定义Exception,如果在调用另一个系统时出现未知错误,那么为什么在这里覆盖其他构造函数被认为是好的,如果我只需要有关失败系统的信息以及导致它的操作:
public class ExternalSystemCallException : Exception {
public string FailedSystem { get; }
public string OperationName { get; }
public ExternalSystemCallFailedException(string failedSystem, string operationName) {
FailedSystem = failedSystem;
OperationName = operationName;
}
}
Run Code Online (Sandbox Code Playgroud)
免责声明:我知道我可以传递其他信息,所以这只是一个相当简单的例子.
更新1:
据我理解,我将覆盖所有构造函数,但也添加了Exception所需的所有参数.那是对的吗?
例:
Exception(string failedSystem, string operationName)
: base()
Exception(string failedSystem, string operationName, string message)
: base(message)
Exception(string failedSystem, string operationName, string message, Exception innerException)
: base(message, innerException)
Run Code Online (Sandbox Code Playgroud) 我有两种方法,具有以下签名:
public void DoSomething<T>(T value) { ... }
public void DoSomething<T>(IEnumerable<T> value) { ... }
Run Code Online (Sandbox Code Playgroud)
我试着像这样打电话给第二个:
DoSomething(new[] { "Pizza", "Chicken", "Cheese" });
Run Code Online (Sandbox Code Playgroud)
它仍然跳进了第一个.如何强制它将进入第二种方法呢?例如,通过对通用参数使用where子句?
编辑:
更确切地说:为什么它不起作用,即使我更具体并将重载更改为类似的东西,这将在尝试调用上面显示的方法时导致编译错误:
public void DoSomething<T>(T value) where T : IComparable { ... }
public void DoSomething<T>(IEnumerable<T> value) where T : IComparable { ... }
Run Code Online (Sandbox Code Playgroud) c# ×5
asp.net ×2
ios ×2
.net ×1
.net-core ×1
asp.net-core ×1
c#-8.0 ×1
dnx ×1
exception ×1
generics ×1
inheritance ×1
iqueryable ×1
javascript ×1
lint ×1
objective-c ×1
overloading ×1
phasset ×1
photokit ×1
swift ×1
tslint ×1
typescript ×1
uuid ×1