Some LINQ and WPF Basics

This evening I wanted to get back to my old habits of creating silly spike projects which just test out a particular piece of functionality/feature.

Tonight I have mixed together some WPF forms and binding with some LINQ queries i was learning.

Part 1 - WPF Forms and Binding
What I wanted to learn from this was to create my own ListBox with a custom drawn ListItem, and bind it to a simple List<String>. The catch was I wanted to do this without looking it up on the net thereby forcing myself to learn the framework and in particular the syntax around the Binding interpreter because there’s no IntelliSense for any Binding properties. The result XAML is below:

   1:  <StackPanel>
   2:      <Button Click="Button_Click">Find With Vowels > 5Button>
   3:      <Button Click="Button_Click_1">Find Specific FileButton>
   4:      <Button Click="Button_Click_2">Find with GroupingButton>
   5:      <TextBlock>TextBlock>
   6:      <ListBox ItemsSource="{Binding}" Height="326">
   7:          <ListBox.ItemTemplate>
   8:              <DataTemplate>
   9:                  <Label Content="{Binding Path=.}" />
  10:              DataTemplate>
  11:          ListBox.ItemTemplate>
  12:      ListBox>
  13:  StackPanel>



Part 2 - LINQ Basics
Although I was watching the video, I went off and did my own thing for the most part, and was playing around with using LINQ to select a list of directories which matched a certain criteria. This sample demonstrated how to add any method as part of the filter condition for a LINQ query, and a brief touch on ordering.

   1:  private void Button_Click(object sender, RoutedEventArgs e)
   2:  {
   3:      var files = from d in new DirectoryInfo(@"C:\Program Files").GetDirectories()
   4:                  where d.Name.Contains("Microsoft")
   5:                  && NumberOfVowels(d.Name) > 5
   6:                  orderby d.Name.Length descending
   7:                  select d;
   8:      this.DataContext = files;
   9:  }
  10:   
  11:  private int NumberOfVowels(string directoryName)
  12:  {
  13:      char[] vowelArray = new char[] { 'a', 'e', 'i', 'o', 'u' };
  14:      var vowels = from ch in directoryName.ToLower()
  15:                   where vowelArray.Contains<char>(ch)
  16:                   select ch;
  17:      return vowels.Count<char>();
  18:  }



Part 3 - LINQ Objects

This idea is to use LINQ to return an anonymous type which is defined inline of the LINQ query itself. (ignore the bad naming convention, here ;) )

   1:  private void Button_Click_1(object sender, RoutedEventArgs e)
   2:  {
   3:      var files = from d in new DirectoryInfo(@"c:\program files").GetDirectories()
   4:                  from f in d.GetFiles()
   5:                  where f.Name == "readme.htm"
   6:                  select new { fileName = f.Name, dirName = d.Name };
   7:   
   8:      this.DataContext = files;
   9:  }



Part 4 - LINQ Grouping
This exercise was to play around with grouping of LINQ queries and the ways that you can select out of the grouped query result. I still haven’t got the knack of this one, just yet….

   1:  private void Button_Click_2(object sender, RoutedEventArgs e)
   2:  {
   3:      var files = from d in new DirectoryInfo(@"c:\program files").GetDirectories()
   4:                  from f in d.GetFiles()
   5:                  group f by new { ext = f.Extension, fullName = d.FullName } into items
   6:                  orderby items.Count() descending, items.Key.ext ascending
   7:                  select new { ext = items.Key.ext, dirName = items.Key.fullName, Count = items.Count() };
   8:   
   9:      this.DataContext = files;
  10:  }


That just about wraps it up for tonight….Not a bad way to spend my time offline


Related posts:

  1. LINQ Raytracer written in 1 statement. OMFG. I think that just about sums up the Fully...
  2. Thread Safety and Locking I was recently reading a post about writing non-threadsafe code...

Leave a Reply


The Tomes Of Experience - powered by WordPress (themed by selder) 0.745 seconds.
17:27:47 06-01-2009