0

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?

Tester
  • 2,887
  • 10
  • 30
  • 60

1 Answers1

0

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;
}
galenus
  • 2,087
  • 16
  • 24
  • But does this take the string query from the Textbox `x:Name="SearchTB"`? – Tester Oct 27 '13 at 15:08
  • So your main problem is in getting the string from the text box? – galenus Oct 27 '13 at 16:52
  • See the change above to the click function. I want to use the string entered in the textbox, to find all the groups in the list whose `Name` is matched or similar to the string enetered – Tester Oct 27 '13 at 16:56
  • See the updated answer. If I understand right, it's very easy, the thing you want. – galenus Oct 27 '13 at 19:22
  • I get an error: `Error 1 'object' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) ` – Tester Oct 27 '13 at 21:18
  • This is because the list is being saved in the DefaultViewModel in an untyped manner. See the correction I made. – galenus Oct 28 '13 at 04:50
  • I actually sorted most of it out. I have problem with the linq query however. http://stackoverflow.com/questions/19627465/filter-list-with-linq-for-similar-items – Tester Oct 28 '13 at 05:02
  • You should do the comparison using the *CompareInfo* if you want to support languages other than English. – galenus Oct 28 '13 at 06:56