简体   繁体   中英

Search List in Xaml with C# using Linq

I am using a MVC Web Api, in which I can get a list of items in my windows store app client.

I can view the list of items on the windows store app using this code:

    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("http://localhost:12345/api/items");

var sampleDataGroups = new List<SampleDataGroup>();


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

                // CL: Parse 1 Item from the content
               var item = JsonConvert.DeserializeObject<dynamic>(content);
                //IEnumerable<string> item = JsonConvert.DeserializeObject<IEnumerable<string>>(content);

                foreach (var data in item)
                {
                      var dataGroup = new SampleDataGroup
                            (

                                (string)data.Id.ToString(),
                                (string)data.Name,
                                (string)"",
                                (string)data.PhotoUrl,
                                (string)data.Description

                            );
                                 sampleDataGroups.Add(dataGroup);
                }
             }
            else
            {
                MessageDialog dlg = new MessageDialog("Error");
                await dlg.ShowAsync();
            }


            this.DefaultViewModel["Groups"] = sampleDataGroups;

The data is received in this json format, that is, data for each item in the list

data    {
  "Id": 1,
  "Name": "bat",
  "Description": "lorem ipsum",
  "Price": 1.39,
  "Weight": "75g",
  "Photo": "test.png",
  "ItemList": null
}

dynamic {Newtonsoft.Json.Linq.JObject}

This maps to the SampleDataGroups structure in the touch app:

public class SampleDataGroup : SampleDataCommon
    {
        public SampleDataGroup()
        {

        }

        public SampleDataGroup(String uniqueId, String title, String subtitle, String imagePath, String description)
            : base(uniqueId, title, subtitle, imagePath, description)
        {
            Items.CollectionChanged += ItemsCollectionChanged;
        }
}

I want to create a search items feature on my windows app, for this I created a search facility in xaml by adding a textbox and button control.

<TextBox x:Name="SearchTB" VerticalAlignment="Top" Text="Search our Products"   Grid.Column="1"Width="420" Height="50" />

<Button x:Name="Product_Search" Content="Go" Grid.Column="2"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="60" Height="50" Margin="0 0 0 0" Click="Product_Search_Click" />

I want to use a linq query to get and return all the items that match the string entered in the textbox when the button is clicked.

I created this function below for when the button is clicked. The string query parameter is meant to be the string entered in the textbox. Any items that are similar to the string entered in the textbox, should be returned.

private void Item_Search_Click(object sender, RoutedEventArgs e)
{
       var querystr = SearchTB.Text; 

}

How do I get the list of items to display on click of the button using the string entered in the search textbox, using Linq?

You should probably do something like:

private void Item_Search_Click(object sender, RoutedEventArgs e)
{
    var groups =  DefaultViewModel["Groups"] as IEnumerable<SampleDataGroup>;
    if (groups == null) 
        return;

    DefaultViewModel["FilteredGroups"] =
       groups 
            .Where(group => ContainsQueryString(group. Title, Search.Text))
            .ToList();
}

private static bool ContainsQueryString(string property, string query)
{
    //performs comparison based on the current UI culture, case insensitive
    //from here: http://stackoverflow.com/questions/444798/case-insensitive-containsstring
    return CultureInfo.CurrentUICulture.CompareInfo.IndexOf(property, query, CompareOptions.IgnoreCase) >= 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM