我试图在Linq表达式中解构一个元组
// somewhere inside another method
var result = from word in words
let (original, translation) = Convert(word)
select original
Run Code Online (Sandbox Code Playgroud)
这是返回元组的方法的签名
(string Original, string Translation) Convert(DictionaryWord word)
{
// implementation
}
Run Code Online (Sandbox Code Playgroud)
但它不是一个有效的语法.我只能在没有解构的情况下访问元组值:
var result = from word in words
let result = GetWord(word, mode)
select result.Original
Run Code Online (Sandbox Code Playgroud)
是否有正确的方法来解构它或Linq表达式中不支持它?
新的TypeScript异步/等待功能使用ES6承诺.AngularJS使用$q
略有不同的接口的服务承诺.
有没有办法使用TypeScript async/await功能与$q
服务承诺?
我想Startup.Configure
使用MVC
默认情况下用于操作的相同方法在方法内设置 API 路由,没有Route
属性。我尝试以下操作:
// API
routes.MapRoute(
name: "api",
template: "api/{controller}/{id?}");
// default
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
Run Code Online (Sandbox Code Playgroud)
控制器看起来像这样
public class CategoriesController : Controller
{
[HttpGet]
public async Task<Category[]> Get(int currentPageNo = 1, int pageSize = 20)
{
// return data here
}
}
Run Code Online (Sandbox Code Playgroud)
但它与这个 url: 不匹配/api/categories
。改为应用下一条规则。如果我使用这样的属性路由
[Route("api/[controller]")]
public class CategoriesController : Controller
{
[HttpGet]
public async Task<Category[]> Get(int currentPageNo = 1, int pageSize = …
Run Code Online (Sandbox Code Playgroud) 我有两个具有外键关系的实体:product
和category
。
@Entity(primaryKeys = "id")
public class Product {
public final long id;
@NonNull
public final String name;
@ForeignKey(entity = Category.class, parentColumns = "id", childColumns = "categoryId")
public final long categoryId;
public Product(long id, @NonNull String name, long categoryId) {
this.id = id;
this.name = name;
this.categoryId = categoryId;
}
}
@Entity(primaryKeys = "id")
public class Category {
public final long id;
@NonNull
public final String name;
public Category(long id, @NonNull String name) {
this.id = id; …
Run Code Online (Sandbox Code Playgroud) 我按照本指南https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods来使用默认接口实现功能。我复制了一段代码,该代码在接口中定义了默认实现IA
,然后在接口中覆盖它IB
:
interface I0
{
void M() { Console.WriteLine("I0"); }
}
interface I1 : I0
{
override void M() { Console.WriteLine("I1"); }
}
Run Code Online (Sandbox Code Playgroud)
但它给出了一个错误CS0106 The modifier 'override' is not valid for this item
和一个警告CS0108 'I1.M()' hides inherited member 'I0.M()'. Use the new keyword if hiding was intended
。TargetFramework
设置为net5.0
,LangVersion
是latest
。为什么即使官方文档中描述了它也不起作用?
我有Product
实体和嵌套Variation
实体的索引.Product
实体包括Id
,Title
和嵌套的变化.Variation
实体组成的Color
,Size
和Price
领域.我需要通过聚合搜索结果Color
,Size
和Price
字段,以获取产品数量为每种颜色,尺寸和价格组.如果我对这些字段使用嵌套聚合,则会获得正确的buckes,但桶中的文档数是Variation
每个桶的实体数.但我需要获得Product
每个桶的实体数量(根文档).
例如,第一个产品有变化(红色,小,10美元),(绿色,小,10美元),(红色,中等,11美元),第二个产品有变化(绿色,中等,15美元).嵌套聚合返回2 for red
和2,small
因为2个变体具有red
颜色和small
大小.但是我需要每个桶的产品数量(根实体),应该是1 red
和1 small
.
由于其他要求,我也不能使用子文档而不是嵌套文档.
如何撰写查询以获得此结果?
这是映射:
{
"product": {
"properties": {
"id": {
"type": "long"
},
"title": {
"type": "string"
},
"brand": {
"type": "string"
},
"variations": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"colour": {
"type": …
Run Code Online (Sandbox Code Playgroud) 我有一个LiveData
依赖于另一个对象LiveData
。据我了解,Transformations.switchMap
应该允许将它们链接起来。但是switchMap
处理程序仅触发一次,并且不会对进一步的更新做出反应。相反,如果我observe
在第一个对象上使用,并在准备好第二个对象时检索它,它就可以正常工作,但在这种情况下,我必须使用Activity
而不是ViewModel
。是否可以LiveData
像一样链接对象,Transformations.switchMap
但不仅可以接收第一个更新,还可以接收所有更新?
这是尝试使用switchMap
:
LiveData<Resource<User>> userLiveData = usersRepository.get();
return Transformations.switchMap(userLiveData, resource -> {
if (resource.status == Status.SUCCESS && resource.data != null) {
return apiService.cartItems("Bearer " + resource.data.token);
} else {
return AbsentLiveData.create();
}
});
Run Code Online (Sandbox Code Playgroud)
这是observe
in活动的一种方法(可以工作,但需要保持逻辑活动):
viewModel.user().observe(this, x -> {
if (x != null && x.data != null) {
viewModel.items(x.data.token).observe(this, result -> {
// use result
});
}
});
Run Code Online (Sandbox Code Playgroud) android android-livedata android-architecture-lifecycle android-architecture-components
我想为活动和非活动菜单项显示不同的标记,以便活动菜单项不包含a
标签:
<li>
<a routerLink="/abc">Inactive</a>
</li>
<li>Active</li>
Run Code Online (Sandbox Code Playgroud)
该routerLinkActive
指令在这里没有帮助,因为它只能为活动路由添加一些类,而不能使用不同的标记。我知道我可以注入Router
我的组件并使用类似的东西
<li>
<ng-container *ngIf="router.isActive('/abc')">Active</ng-container>
<a *ngIf="!router.isActive('/abc')" routerLink="/abc">Inactive</a>
</li>
Run Code Online (Sandbox Code Playgroud)
但对于这种情况有更好的内置解决方案吗?
c# ×3
android ×2
aggregation ×1
android-architecture-components ×1
android-architecture-lifecycle ×1
android-room ×1
angular ×1
angularjs ×1
asp.net-core ×1
async-await ×1
c#-7.0 ×1
javascript ×1
linq ×1
routes ×1
routing ×1
tuples ×1
typescript ×1