Searching Record in Windows Store App Using LINQ

Introduction

Today we will create a search query operation in a Metro style application with the help of LINQ. Since in our last article we dynamically add books detail record in GridView, so in this article we will create the ability to search these book details based on a full or partial book name as user input and related records will be shown in the GridView as search results.

In the following we are including the entire code of the XAML file and the code behind file to create this mini application. 

Step 1 : First, you will create a new Metro Style Application. Let us see the description with images of how you will create it.

  • Open Visual Studio 2012
  • File -> New -> Project
  • Choose Template -> Visual C# -> Metro Style Application
  • Rename the application

img1.gif

Step 2 : In the Solution Explorer there are two files that we will primarily work with; the MainPage.xaml and MainPage.xaml.cs files.

img2.gif

Step 3 : The MainPage.xaml file is as in the following code:

Code :

<Page

    x:Class="App10.MainPage1"

    IsTabStop="false"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:App10"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="Red">

        <Grid.RowDefinitions>

            <RowDefinition Height=".133*"></RowDefinition>

            <RowDefinition Height=".333*"></RowDefinition>

        </Grid.RowDefinitions>

        <Grid Grid.Row="0">

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width=".333*"></ColumnDefinition>

                <ColumnDefinition Width=".023*"></ColumnDefinition>

                <ColumnDefinition Width=".333*"></ColumnDefinition>

            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>

                <RowDefinition Height=".333*"></RowDefinition>

                <RowDefinition Height=".333*"></RowDefinition>

            </Grid.RowDefinitions>

            <Button Content="Search Book" Background="Blue" Grid.Column="2" Margin="276,37,0,21" Click="Button_Click_1"></Button>

            <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Text="Search Book and its Detail using below Criteria" FontSize="30" FontWeight="ExtraBold" HorizontalAlignment="Center"></TextBlock>

            <TextBlock Text="Enter Full or part of Book Name" FontSize="20" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right" FontWeight="Bold"></TextBlock>

            <TextBox x:Name="txt1" Grid.Column="2" Width="200" Height="50" HorizontalAlignment="Left" Margin="0,83,0,59" Grid.RowSpan="2"/>

           </Grid>

       <GridView x:Name="prodView"  Width="600" Height="800" ScrollViewer.VerticalScrollBarVisibility="Visible"

            Canvas.Left="240" ItemsSource="{Binding}" Grid.Row="1" Header="Search Result" FontSize="40" FontWeight="ExtraBold"  Visibility="Collapsed" Background="blue">

            <GridView.ItemsPanel>

                <ItemsPanelTemplate>

                </ItemsPanelTemplate>

            </GridView.ItemsPanel>

            <GridView.ItemTemplate>

                <DataTemplate>

                    <StackPanel Orientation="Vertical">

                        <TextBlock Text="{Binding _bookname}" Width="600" Height="50"  FontSize="30" FontWeight="Bold"  />

                        <TextBlock Text="{Binding _authorname}" Width="600" Height="50"  FontSize="30" FontWeight="Bold"  />

                        <TextBlock Text="{Binding _bookprice}" Width="600" Height="50"  FontSize="30" FontWeight="Bold" />

                    </StackPanel>

                </DataTemplate>

            </GridView.ItemTemplate>

        </GridView>

    </Grid>

</Page>

Step 4 : We will add a class for the book behavior attributes:

Code :

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace App10

{

    class Class1

    {

        string bookname;

        string authorname;

        string bookprice;

        public string _bookname

        {

            get { return bookname; }

            set { bookname = value; }

        }

        public string _authorname

        {

            get { return authorname; }

            set { authorname = value; }

        }

        public string _bookprice

        {

            get { return bookprice; }

            set { bookprice = value; }

        }

 

        public Class1()

        { }

        public Class1(string s, string r, string p)

        {

            bookname = s;

            authorname = r;

            bookprice = p;

        }

 

    }

}

 

Step 5 : The MainPage.xaml.cs file is as in the following code:

Code :

 

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace App10

{

    public sealed partial class MainPage1 : Page

    {

        List<Class1> bookinfo = new List<Class1>();

        Class1 bookObj;

        public MainPage1()

        {

            this.InitializeComponent();

            storeData();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        public void storeData()

        {

            bookObj = new Class1();

            bookObj._authorname = "Author Name: Shiv Prasad Koirala";

            bookObj._bookname = "Book: C# Fundamental";

            bookObj._bookprice = "Price: 350";

            bookinfo.Add(bookObj);

 

            bookObj = new Class1();

            bookObj._authorname = "Author Name: Bala Guruswami";

            bookObj._bookname = "Book: C and C++ Fundamental";

            bookObj._bookprice = "Price: 300";

            bookinfo.Add(bookObj);

 

            bookObj = new Class1();

            bookObj._authorname = "Author Name: Chetan Bhagat";

            bookObj._bookname = "Book: Three Mistake of My Life";

            bookObj._bookprice = "Price: 200";

            bookinfo.Add(bookObj);

 

            bookObj = new Class1();

            bookObj._authorname = "Author Name: Steven Holzner";

            bookObj._bookname = "Book: Java Black Book";

            bookObj._bookprice = "Price: 400";

            bookinfo.Add(bookObj);

 

            bookObj = new Class1();

            bookObj._authorname = "Author Name: Pradeep Khuswaha";

            bookObj._bookname = "Book: Core Java Fundamental";

            bookObj._bookprice = "Price: 200";

            bookinfo.Add(bookObj);

        }

 

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            string searchvar = txt1.Text;

            IEnumerable<Class1> Books = from obj in bookinfo where obj._bookname.Contains(searchvar) select new Class1(obj._authorname, obj._bookname, obj._bookprice);

 

            prodView.DataContext = Books;

            prodView.Visibility = Visibility.Visible;

        }

    }

}

 

Step 6 : After running this code the output looks like this:

img3.gif

Search from the full name of the book:

img4.gif

Search as a part of the name:

img5.gif


Similar Articles