简体   繁体   English

使用Linq使用C#在Xaml中搜索列表

[英]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. 我正在使用MVC Web Api,在其中可以获取Windows Store应用程序客户端中的项目列表。

I can view the list of items on the windows store app using this code: 我可以使用以下代码在Windows应用商店应用中查看商品列表:

    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 数据以json格式接收,即列表中每个项目的数据

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: 这映射到触摸应用程序中的SampleDataGroups结构:

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. 我想在Windows应用程序上创建搜索项功能,为此,我通过添加文本框和按钮控件在xaml中创建了搜索工具。

<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. 我想使用linq查询来获取并返回与单击按钮时在文本框中输入的字符串匹配的所有项目。

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. string query参数是在文本框中输入的字符串。 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? 我如何使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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