A Journey Into Expressions

I have been a little obsessed with LINQ and Func<T,TResult> of late and finally managed to find some time to do a little bit of exploration into the System.Linq.Expressions namespace.

I have published an article over at codeproject (http://www.codeproject.com/KB/linq/Expressions.aspx) which tells you all about how to create the following

lamda expression : (x) => x.Length > 1 , entirely in code using the System.Linq.Expressions namespace.

In case you are curious this is what this expression looks like when working with the System.Linq.Expressions namespace:

 

   1:  ConstantExpression constLength = Expression.Constant(1);
   2:   
   3:  ParameterExpression stringParameter = 
   4:      Expression.Parameter(typeof(String), "s");
   5:   
   6:  MemberExpression stringMember = 
   7:      LambdaExpression.PropertyOrField(stringParameter, "Length");
   8:   
   9:   
  10:  Expression<Func<String, Boolean>> bindLambda =
  11:      Expression.Lambda<Func<String, Boolean>>
  12:      (
  13:          Expression.GreaterThan(
  14:          stringMember,
  15:          constLength),
  16:          stringParameter
  17:      );
  18:   
  19:   
  20:  foreach (String bindingString in 
  21:      bindings.Where(bindLambda.Compile()))
  22:  {
  23:     .....
  24:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

In the article I obvisouly talk in a lot more detail, so if you want to know more, just have a look at the full article http://www.codeproject.com/KB/linq/Expressions.aspx

Advertisements

12 thoughts on “A Journey Into Expressions

  1. […] Sacha Barber goes in-depth with A Journey Into Expressions […]

  2. […] A Journey Into Expressions (Sacha Barber) […]

  3. Yogesh says:

    Hi Sacha,

    I didn’t knew how to contact you so I posted here.

    I am really stuck at some point. Can you help? Here is what is bothering me…

    Take this example in XML:

    How can I achieve the same effect for width in the codebehind?

    I tried double.NaN & 0, but both didn’t helped.

  4. Yogesh says:

    The comment parsed removed the code I posted as XAML example. I am replacing the opening and closing tags with their XML counterparts:

    <toolkit:DataGridTextColumn Header=”Alias” Width=”*”
    Binding=”{Binding GroupAlias}” IsReadOnly=”True” />

  5. sacha says:

    Yogesh I don’t know what type of thing a <toolkit…./> object is, but if it is anything like the Grid in WPF which uses * notation, you should do something like

    Width = new GridLength(1,GridUnitType.Star);

    Hope that helps

  6. Yogesh says:

    Wow man… Thanks a lot. You rock. 🙂

    PS: It is the WPF toolkit which is just released. So in my case, it is Width = new DataGridLength(1, DataGridLengthUnitType.Star). Thanks again man. 🙂

  7. sacha says:

    Whats in the Toolkit, haven’t seen it yet. Is that the DataGrid etc etc

  8. Yogesh says:

    Yes. Contains these controls…
    1. DataGrid
    2. Calendar
    3. Date time picker
    4. Ribbon control

    I found the library to be good. Although there are some errors I faced, and no one replied on the discussion threads, but still, I think this is real good stuff. Just adding a Today button in the Calendar has such work cut in.

    I think you should check it out too.

  9. sacha says:

    Thanks for letting me know man, I have actually seen the grid before. We use Infragistics grid at work. GRRR

  10. Yogesh says:

    I too was using the Xceed free version, but I gave it up for this grid as the Xceed free version is almost same as this one. I am too short of money to buy a commercial grid. Although it contains a lot of errors as of now, I think that this grid might eventually match all the goodies offered by commercial grids.

    BTW, both Infragistics’s and Xceed’s grid are really powerful.

  11. sacha says:

    Yeah, truth be known I am not that keen on Infragistics grids internal behvaiour, but it looks good.

  12. Peter says:

    I am greenhorn about the multi-threading. I want to ask you that multi-threading whether can enhance the efficency of program(in other words, multi-threading is more efficient than single threading or not)? Why?

    Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: