Get a Value from ObservableCollection based on a condition

jrahma 111 Reputation points
2021-03-03T23:21:02.293+00:00

Hi,

I have below code where I populate stages from a database but I want to conditionally get stage_value where stage_id is equal to xxxx.

Here is my Class:

public class UserStagesData
{
public int user_stages_id { get; set; }
public string stage_id { get; set; }
public int stage_value { get; set; }
}

and this is my populating data code and I have marked where I want to read the value:

public async void populate_user_stages()
{
try
{
var client = new HttpClient();

    client.BaseAddress = new Uri("https://www.domain.com/PopulateStagesData.php");

    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("MyUserID", 1)
    });

    var response = await client.PostAsync("https://www.domain.com/PopulateStagesData.php", content);

    if (response.IsSuccessStatusCode)
    {
        var data = await response.Content.ReadAsStringAsync();

        var result = JsonConvert.DeserializeObject<List<UserStagesData>>(data);
        ObservableCollection<UserStagesData> trends = new ObservableCollection<UserStagesData>(result);

        **// Show Value Where stage_id = "PA"**
        await App.Current.MainPage.DisplayAlert("PA", trends.Where(x => x.stage_id == "PA").ToString(), "Ok");
        **// Show Value Where stage_id = "PS"**
        await App.Current.MainPage.DisplayAlert("PS", trends.Where(x => x.stage_id == "PS").ToString(), "Ok");
        **// Show Value Where stage_id = "SA"**
        await App.Current.MainPage.DisplayAlert("SA", trends.Where(x => x.stage_id == "SA").ToString(), "Ok");
        **// Show Value Where stage_id = "SD"**
        await App.Current.MainPage.DisplayAlert("SD", trends.Where(x => x.stage_id == "SD").ToString(), "Ok");

        ScrollViewSettingsMyStages.IsVisible = true;

        BusyIndicatorMyStages.IsBusy = false;
        ButtonSettingsMyStages.IsVisible = true;
    }
    else
    {
        await App.Current.MainPage.DisplayAlert("Error", "Error", "Error");
    }
}
catch (Exception ex)
{
    Crashes.TrackError(ex, new Dictionary<string, string>
    {
        { "Page", "Members" },
        { "Where", "PopulateAllMembers" }
    });

    return;
}

}

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,342 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-03-04T03:01:28.353+00:00

    Hello,

    Welcome to Microsoft Q&A!

    We could get the model data with Enumerable.First Method depending on stage_id .

    Modify your code as below .

       string xxxx ;  
       DisplayAlert(xxxx , trends.Where(x => x.stage_id == xxxx ).First()?.stage_value.ToString(), "Ok");  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments