Posts Tagged ‘LINQ’

LINQ Raytracer written in 1 statement.

August 14th, 2008

OMFG.

I think that just about sums up the Fully LINQified Raytracer….I missed this when it hit the blogosphere back in October last year, but wow i’m just gobsmacked….

I found this while reading about Mono’s 100% compliance with .NET 3.0

I feel so suddenly inadequate in what I do…..

Some LINQ and WPF Basics

August 12th, 2008

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