从查询firebase返回多个实例



目前正在用Xamarin表单(c#(构建应用程序。该应用程序有一个firebase后端。用户可以在应用程序中提交存储在firebase上的日志。

我正在开发的当前功能将基于用户ID(uID(从服务器检索所有日志实例,然后将它们显示在屏幕上。我目前的解决方案只返回最新的日志实例:

public async void getLastLogInstance(Guid uID)
{
//fetch last log user had information provided by user
var getUser = (await firebaseClient
.Child("UserLogs")
.OnceAsync<UserLogs>()).Where(a => a.Object.UserID == uID).LastOrDefault();

//if getUser equals null then the user has no log history
if (getUser == null)
{
logDate.Text = "No recent logs found";
}
//valid logs found, return data, successful login
else
{
var Content = getUser.Object as UserLogs;
logDate.Text = Content.logTime;
logData.Text = Content.logData;
happinessRating.Text = Content.sliderValue;

}

}

我需要访问已返回的各个属性,例如Content.logTime、Content.logData

我已经找到了一种返回多个实例的方法,但是我无法访问上面提到的单个属性。以下代码取自另一个具有不同用途的模块,但该模块的用途与上的类似

public async void DisplayContent(Guid idAsGuid)
{
try
{
/*getInstance will hold a list 
* of result instances with the matching userID
* */
getResults = (await firebaseClient
.Child("phq9Results")
.OnceAsync<phq9Results>()).Where(a => a.Object.UserID == idAsGuid).Select(item => new phq9Results
{
UserID = item.Object.UserID,
overallResult = item.Object.overallResult,
submissionDate = item.Object.submissionDate
}).ToList();
//order list so most recently added result is positioned first
var orderedResults = getResults.OrderByDescending(x => x.submissionDate).ToList();
listOfResults.ItemsSource = orderedResults;

}
catch
{
return;
}

非常感谢您提前提供的帮助!!

原来是一个相当简单的解决方案

通过进行读取您试图读取的数据的所有实例

//fetch all logs connected to the users ID (uID)
var getUser = (await firebaseClient
.Child("UserLogs")
.OnceAsync<UserLogs>()).Where(a => a.Object.UserID == uID).Select(item => new UserLogs
{
sliderValue = item.Object.sliderValue,
}).ToList();

然后循环通过getUser列表,通过item.x 访问属性

//loop for each item in object
foreach (var item in getUser)
{
total = total + item.sliderValue
}

最新更新