= fn(n){n xor 0xFFFF004B eq 0×0 ? n : fn(n – 0×1)}

Gd morning Portland

April 2nd, 2010

Hi, we r coming!! Live report is coming up soon!


Light house:

Great Ocean Road:

Bookmark and Share


Dancing Queen – Oh, Mamma Mia!

March 21st, 2010

What a funny story! Today I went to Her Majesty Theatre, watched the most famous musical in the world – Mamma Mia!

I love the theme song, Dancing Queen. :-)

Bookmark and Share


Hailstorm Late Updates

March 11th, 2010

All photos are copy-right by David Jones.

Bookmark and Share


Use of Lambda Expression to Create Complicated Query

March 10th, 2010

.NET FX introduces a powerful weapon for creating the compact, breezy and elegant code in your project. The weapon’s name is as complicated as the mathematical stuff, called Lambda expression.

As the name implies, the great convenience comes great complication inside. Lambda expression is too important to understand, it is useful and I think it is good to know how it works. Well, it’s not as complicated as we think, just a little bit confusing when using it.

First of all, Lambda is used for 2 purposes. 1 is to simplify delegation, 2 is to create expression tree. In my case, I need to create lambda expression to create dynamic query.

Imagine the following usecase:

In your product table, there is a field called “Tags” and it’s used for storing the CSV (comma separated value) like “car, mazda, suv, luxury”, you have many records:

Product Table: (Table name: ProductTable)

ID    ProductName    Tags
1    Tow bar        car,mazda,suv,luxury
2    Cup holder    car,luxury
3    Phone holder    car,mazda
4    FM Transmitter    car,suv
5    Queens bed    luxury
6    DVD Burner    car,BMW,x5,luxury

When your searching criteria is
“mazda” and “luxury”, the following records would be selected out:

ID    ProductName    Tags
1    Tow bar        car,mazda,suv,luxury
2    Cup holder    car,luxury
3    Phone holder    car,mazda
5    Queens bed    luxury
6    DVD Burner    car,BMW,x5,luxury

In ancient SQL age, you would probably write a SQL like this:

string SQL = “SELECT * FORM ProductTable WHERE Tags LIKE’%mazda%’ OR Tags LIKE ‘%luxury%’”;

It is IMpossible to create SQL like above except the project scope tells you the fixed keywords that would be used, the keywords should flexible. Then the code would be as much as:

string[] tags = new string[]{“mazda”, “luxury”};
string SQL = “SELECT * FROM ProductTable WHERE 1=1″;
foreach(string tag in tags)
{
SQL += string.format(@” AND Tags like ‘%{0}%’”, tag);
}

You must be very exciting that your code supports as many keywrods as it can. (I did before, I mean I would be very exciting when the code filled with logical stuff, LOL, of course, not now, that’s what geek did before~ )

Back to the reality, to the modern age, with the lambda expression, what you need to do is:

(pre-condition: you have created corresponding domain model which mapped to the database.)
The domain object or entity would look like:

[Table("ProductTable")] Class ProductTable{
[Column]public int Id{get; set;}
[Column]public string ProductName{get; set;}
[Column]public string Tags{get; set;}
}

to fullfill the same kind of logic as the stone-aged people do, you just need to

DataContext db = new DataContext(“<connection-string>”);
Table<ProductTable> table = db.GetTable<ProductTable>();
string tag1 = “mazda”;
string tag2 = “luxury”;
var data = table.where(x=>x.Tags.Contains(tag1)).where(x=>x.Tags.Contains(tag2)) select new ProductTable;
return data.AsQueryable();

Since so far, you still feel comfortable with this, but after minute, you would probably wanna go back stone age, how do we dynamically create the query in this case? Unfortunately, there is no short-cut for this, but many people provide lots of solutions on the Internet. We can either download some source codes about dynamically query on Internet or just grab the codes from the blogger’s website.( Most of time, you can’t use all of their codes posted on the blog, the codes are broken and bad structure, remember what Koumei always suggests, absorb the mind from the other coders, do not copy the code they provided. )

But in here, I would rather to use Expression Tree to implement the dynamic query.

First of all, we need to create an expression, because it would be applied to IQueryable.Where(Expression<Func<ProductTable, bool>> expression), so the expression should be defined as:

public static System.Linq.Expressions.Expression<Func<ProductTable, bool>> ApplyTags(IEnumerable<string> tags) //parameter is the collection of tags
{
ParameterExpression c = Expression.Parameter(typeof(ProductTable), “c”); //Get the parameter from Func<ProductTable, bool>, remember? this is the advanced and lazy version of delegation, we need to craete ParameterExpression to hold this
var tagsProperty = Expression.Property(c, typeof(ProductTable).GetProperty(“Tags”)); //Here is the expression for get the Tags value from ProductTable object
Type[] ContainsTypes = new Type[1];
ContainsTypes[0] = typeof(string);
System.Reflection.MethodInfo myContainsInfo = typeof(string).GetMethod(“Contains”, ContainsTypes);
//Create the expression collection (based on the tags collection from parameter)
List<Expression> myTagExpressions = new List<Expression>();
foreach (var t in tags)
{
myTagExpressions.Add(Expression.Call(Expression.Call(tagsProperty, “ToString”, null, null), myContainsInfo, Expression.Constant(t)));
}
Expression OrExpression = null;
foreach (Expression myTagExpression in myTagExpressions)
{
if (OrExpression == null)
{
OrExpression = myTagExpression;
}
else
{
//Need to implement OR relationship among all the expressions
OrExpression = Expression.Or(myTagExpression, OrExpression);
}
}
//This is how Expression would be extracted to expression tree later on.
Expression<Func<ProductTable, bool>> predicate = Expression.Lambda<Func<ProductTable, bool>>(OrExpression, c);
return predicate;
}

Again, you can’t copy the code, since you would NOT have the same object like ProductTable and the same “Tags” property as I do. Luckily, you still can copy the code that I provide later, which is using the magic of template or generic programming. Before that, let us see how to use it:

IQueryable all = table.Where(ApplyTags(new List{“mazda”,”luxury”})).AsQueryable();

Since so far, I am 100% sure you can do it like refactoring, re-structuring to optimize your code. However, I would like to provide the code which can be general used:

public static System.Linq.Expressions.Expression<Func<T, bool>> ApplyFilter<T>(IEnumerable<string> filters, string property)
{
//Get parameter
ParameterExpression c = Expression.Parameter(typeof(T), “c”);
//Get property from <T>
var tagsProperty = Expression.Property(c, typeof(T).GetProperty(property));
Type[] ContainsTypes = new Type[1];
ContainsTypes[0] = typeof(string);
System.Reflection.MethodInfo myContainsInfo = typeof(string).GetMethod(“Contains”, ContainsTypes);
List<Expression> myTagExpressions = new List<Expression>();
foreach (var t in filters)
{
myTagExpressions.Add(Expression.Call(Expression.Call(tagsProperty, “ToString”, null, null), myContainsInfo, Expression.Constant(t)));
}
Expression OrExpression = null;
foreach (Expression myTagExpression in myTagExpressions)
{
if (OrExpression == null)
{
OrExpression = myTagExpression;
}
else
{
OrExpression = Expression.Or(myTagExpression, OrExpression);
}
}
Expression<Func<T, bool>> predicate = Expression.Lambda<Func<T, bool>>(OrExpression, c);
return predicate;
}

The usage is similiar:

IQueryable all = table.Where(ApplyFilter<ProductTable>(new List{“mazda”,”luxury”}, “Tags”)).AsQueryable();

Not that hard, right?

Enjoy the powerful weapon, but don’t hurt yourself, bacause the deadline is ahead!

6    DVD Burner    car,BMW,x5,luxury
Bookmark and Share


Giant Hail Storm Hits Melbourne at 2pm Saturday

March 6th, 2010

Never seen this before. In the morning I just plan to go outside, but unfortunately or you may say luckily something wrong with my navigation, I could not turn it on! Then I stay home and try to fix my GPS.  About 2pm, it turns a sudden rain, heavily. Soon I heard some noises from the roof, become louder and louder, even deafening. I think the house is gonna break down! My goodness. It is a big hail storm. Hitting the tree, house and the car outside.

The hailstones are no where!

And the power is cut for about 4 hours.

Here is the abridged news from abc.net.au:

Hailstones the size of golf balls have hit the suburb of Melton, in Melbourne’s west, as a line of storms moved through western Victoria.
……………….
Melton, on Melbourne’s northwest fringe,  was one of the first suburbs to feel the storm’s wrath.
(Tell you what, Melton is the place where I plan to go, roughly 35KM from CBD Melbourne.)

Lucky me! I love my GPS, really love it. Actually I blame it why it stops working when I get into the car. But now I understand, it stops to save me from hitting by the hailstorm! And always give me direction when I am lost.

Bookmark and Share


There Is Nothing Meant To Be :)

February 9th, 2010

Do you have an experience that life just feels like a movie show? I bet many people do. Someone comes in your life, someone leaves. I think there would be 1000 of people walk beside you everyday who just passing by. You never know their names. By any chance, you meet someone who is passing by, and catch the name. Soon you guys develop relationship. Do you believe that is the fate?

500 Days of Summer is such a good movie, that let people think something seems meant to be, a decision, a response could easily make things different. But who knows? If everything is meant to be, why should we spending time to explore our life? In the story, Tom believe that the girl,whose name is Summer, is the one of his life, he spends his time with her, hanging out together, they seem like couple, lover. Summer gets merry, but her husband is not Tom. At the end of story, the movie mentions that Tom finally figure out that there is nothing meant to be, and he meets a girl who is a competitor of his new job.  Will Tom fall into another 500 days again? I hope not.

Good story , good movie and it is a real, or much closer to real life. I strongly recommend it.

Bookmark and Share


Home Made Kim Ba (Sushi)

January 8th, 2010

Yummy!

Bookmark and Share


UP – the adventure of a lifetime

January 4th, 2010

All his life is dreaming of adventure, finally, he is taking off!

It is a moving story at the very beginning when he is living with his wife before the exploration. It’s pity that he can’t take his wife who encourage him to take the adventure :(

What’s the story of the movie? Well, I think pixar is gonna tell us, an adventure could be a lifetime, as long as you still dream of it. But it would become a true story when stop dreaming, just take off today!

Bookmark and Share


Live Cast! David’s Wedding Party (China, GMT+8 20:30)

December 31st, 2009

First of all, Happy new year~

Yummy foods and happy David’s big day!

1

4

5

6

(last updated: 31 Dec 09  21:01 GMT+8)

7

(last update: 21:07 GMT+8)

8

You guys are perfectly natural!

(last update: 21:10 GMT+8)

9

(last update: 21:19 GMT+8)

Bookmark and Share


Christmas is here :)

December 24th, 2009

What kind of background music is good for this Christmas? It takes me quite a while to figure it out. After reviewing heaps of Christmas albums, finally my verdict is Diana Krall’s “Christmas Songs” album released on December 2005, but still a good album for this holiday. :) You can easily download from iTune, RRP $16.99 on iTune.

front

I like her voice singing “Have Yourself A Merry Little Christmas”, it is romantic.

Bookmark and Share


Profile

  • Koumei Deng's Facebook profile


Submit Your Site To The Web's Top 50 Search Engines for Free!