Sunday, May 24, 2009

Querying data From Collection using LINQ and Silverlight 2.0.

 

In this lab, we will try to see LINQ (Language Integrated Query). If you are already familiar with LINQ, That will be added advantage here !!

LINQ has different flavours-

1) LINQ to Objects.

2) LINQ to Data. (LINQ to Dataset, LINQ to SQL, LINQ to Entity).

3) LINQ to XML.

If you ask me about LINQ, I will say “I LOVE LINQ” especially when I work with XLINQ.

Any ways, that’s my thinking !!

This demo is will show you how to Query to the objects. So, Let’s get started -

Note-: We will see XLINQ as well in future-

1) Create Silverlight project by the name SLLinqApplication.

2) Add a new class in Silverlight project by the name “Customers.cs” and code as shown bellow-

public class Customers
{
public string CustomerName { get; set; }
public string City { get; set; }
}






3) Now let’s design the screen as bellow-



CollectionDemoScreen



4) Code is as follows-




<UserControl x:Class="SLLinqLab.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="150">
<Canvas x:Name="LayoutRoot" Background="Black">
<StackPanel Orientation="Horizontal" Canvas.Top="5">
<TextBlock FontSize="10" Foreground="White" Text="Choose Operation - " Canvas.Left="10" Canvas.Top="10"/>
<ComboBox x:Name="ChooseOperation" Width="150">
<TextBlock Text="Where City Equal To"/>
<TextBlock Text="Where City Not Equal To"/>
<TextBlock Text="Where City Contains "/>
<TextBlock Text="Where City Not Contain"/>
</ComboBox>
<TextBox x:Name="txtValue" Margin="0,0,0,0" Width="150"/>
</StackPanel>
<Button Content="Show Result" Click="Button_Click" Canvas.Top="30"/>
<StackPanel Orientation="Horizontal" Canvas.Top="70">
<TextBlock FontSize="10" Foreground="White"

Text="See All Customers - " Canvas.Left="0"/>
<ComboBox x:Name="SeeCustomers"

Width="150" ItemsSource="{Binding CustomerName}">

</ComboBox>
</StackPanel>

</Canvas>
</UserControl>






5) Now let’s create Collection to add multiple customers as follows-




public Page()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
List<Customers> custList;
void Page_Loaded(object sender, RoutedEventArgs e)
{
custList = new List<Customers>()
{
new Customers(){CustomerName="AAA",City="Pune"},
new Customers(){CustomerName="BBB",City="Pune"},
new Customers(){CustomerName="CCC",City="Mumbai"},
new Customers(){CustomerName="DDD",City="Mumbai"},
new Customers(){CustomerName="EEE",City="Hydrabad"},
new Customers(){CustomerName="FFF",City="Bangalore"}
};
}






6) Now let’s write code on “Show Result” button click event-




private void Button_Click(object sender, RoutedEventArgs e)
{
switch (ChooseOperation.SelectedIndex)
{
case 0:
var query1 = from customer in custList
where customer.City.Equals(txtValue.Text)
select new Customers { CustomerName=customer.CustomerName};
SeeCustomers.DisplayMemberPath = "CustomerName";
SeeCustomers.ItemsSource = query1.ToList();

break;
case 1:
var query2 = from customer in custList
where !customer.City.Equals(txtValue.Text)
select new Customers { CustomerName = customer.CustomerName };
SeeCustomers.DisplayMemberPath = "CustomerName";
SeeCustomers.ItemsSource = query2.ToList();

break;
case 2:
var query3 = from customer in custList
where customer.City.Contains(txtValue.Text)
select new Customers { CustomerName = customer.CustomerName };
SeeCustomers.DisplayMemberPath = "CustomerName";
SeeCustomers.ItemsSource = query3.ToList();
break;
case 3:
var query4 = from customer in custList
where !customer.City.Contains(txtValue.Text)
select new Customers { CustomerName = customer.CustomerName };
SeeCustomers.DisplayMemberPath = "CustomerName";
SeeCustomers.ItemsSource = query4.ToList();

break;
}
}





7) 


That’s all !! you are done with the assignment !!



Now as you have completed writing the eleventh Silverlight Lab, let’s have a closer look on twelve lab “Creating and using Splash Screen in Silverlight 2.0.


No comments:

Post a Comment