c# - 我想在我的 asp.net web api 应用程序中按 id 过滤 firebase realtime-database,但我不知道如何构建查询
问题描述
我正在开发一个带有 firebase 实时数据库的 asp.net web api 项目。我使用 firebaseclient 库。但我想知道如何使用 firebase 客户端开发查询。
我已经创建了获取所有供应商的查询。这是工作。
另一个通过 id 获取供应商。它给出了语法错误。这是行不通的。
//method to get all suppliers. Working properly
[Route("getAll")]
public async Task<HttpResponseMessage> GetSuppliers()
{
var firebaseClient = new FirebaseClient(<firebase url>);
//Retrieve data from Firebase
var suppliers = await firebaseClient
.Child("suppliers")
.OnceAsync<SupplierModel>();
List<SupplierModel> supplierList = new List<SupplierModel>();
foreach (var sup in suppliers)
{
supplierList.Add(sup.Object);
}
HttpResponseMessage response;
response = Request.CreateResponse(HttpStatusCode.OK, supplierList);
return response;
}
//method to filter by id. Have syntax errors
[Route("addSuppllier/{supplierId}")]
public async Task<HttpResponseMessage> GetSupplierById([FromUri] string supplierId)
{
var firebaseClient = new FirebaseClient(<firebase url>);
//Retrieve data from Firebase
var suppliers = await firebaseClient
.Child("suppliers")
.OrderBy("supplierId")
.EqualTo(supplierId);
List<SupplierModel> supplierList = new List<SupplierModel>();
foreach (var sup in suppliers)
{
supplierList.Add(sup.Object);
}
HttpResponseMessage response;
response = Request.CreateResponse(HttpStatusCode.OK, supplierList);
return response;
}
第一个查询正确检索所有供应商。第二个查询有语法错误。
解决方案
您可以尝试使用这种方法。
FirebaseResponse response = await firebaseClient.GetAsync("suppliers");
JObject jsonResponse = response.ResultAs<JObject>();
var query = new List<SupplierModel>();
if (jsonResponse != null)
{
foreach (var item in jsonResponse)
{
var value = item.Value.ToString();
var supplierModel = JsonConvert.DeserializeObject<SupplierModel>(value);
//note: supplier model should have id attribute
supplierModel.Id = item.Key;
query.Add(supplierModel);
};
}
如果不为空,则按Id过滤查询。
推荐阅读
- react-native - 如果在反应原生 android 的水平平面列表行中添加 WebView 不可滚动
- django - 从多表中选择原始数据
- mysql - 当 Sequelize.findAll 在另一个文件上时,导出函数不返回数据
- java - 在 NetBeans 上使用 JDK 13 运行 Java 应用程序
- c - 在命令行读取表达式
- list - 关于循环时需要将所有原子转换为单项列表
- elasticsearch - 我可以将 Elasticsearch 中的字段类型同时设置为 ip 和 ip_range 吗?
- sql - 如何在sql中计算销售和回报的百分比
- docker - RUN 命令抛出“未找到”
- c# - 我想在我的应用程序上有一个边框突出显示动画