Craig Mattson (Personal Website)
Home - Blog, News, About MePrograms - C#.Net, Java, VB6MusicWebsites

My Blog

LINQ - You know... just another acronym... (29/03/2009 08:43:45 PM)

crystal_clear_app_kedit_128Since the .NET Framework 3.5 was released, one buzzword floating around was LINQ (Language Integrated Query). I didn't take much note of it until tonight when I was looking for more jobs to apply for. So anyway, for those of you who haven't (and are C# or just .NET developers), this may be an interesting read for you.

What is LINQ?

If you've played with an ANSI SQL99 compliant database (read: MySQL, Oracle, MSSQL etc...), you will understand the following code:

SELECT * FROM table1 WHERE column1=expression ORDER BY column1 ASC;

The idea of LINQ is to take a similar structure and apply it to an array or list of objects. The concept though of LINQ isn't to create a list of methods (although the Language Integrated component acts as a pointer to a set of methods which I will discuss later), but to add keywords into the language to flow it similar to SQL.

Example Please!

The first and probably most easy example is to create a standard 1D array of strings. Lets assume we have Victorian suburbs / towns as the names:

string[] towns = { "Neerim South", "Caulfield", "Clayton", "Pakenham", "Drouin", "Warragul" };

These are in no particular order - so I will use LINQ to sort these into alphabetical order. However, LINQ (as far as I can tell) is only usable if the object uses the interface IEnumerable. This interface basically allows you to implement foreach iterations without requiring you to set up counters etc... Anyway, if you are using IEnumerable (or an object that implements the IEnumerable interface), you can use the following code:

IEnumerable sortedTowns = from t in towns order by t select t;

So that's just it. If you were to put a where clause in, you would do it between the from and orderby. From here, the data is sorted in alphabetical order and will return the t as the string.

What about classes?

Lets assume I have an object called Item with properties Name and Age. I will create 4 objects:

List i = new List();
i.Add(new Item() { Name = "Craig", Age = 21 });
i.Add(new Item() { Name = "Arkady", Age = 24 });
i.Add(new Item() { Name = "Claudia", Age = 21 });
i.Add(new Item() { Name = "Daniel", Age = 17 });
i.Add(new Item() { Name = "Benjamin", Age = 21 });

So now that I have my objects, lets create a query:

IEnumerable query = from item in i where item.Age > 18 orderby item.Name select new Item() { Name = item.Name, Age = item.Age };

What this does is iterate through the list and we are returning an Item (as defined by the ). Now, just like any IEnumerable class, you can use a foreach to iterate through:

foreach(Item itm in i)
    // Handle the Item

So how does it work again?

Quite simply, the keywords are mapped to methods in the IEnumerable interface. That is; OrderBy<>, Where<>, Select<> etc... are actually methods that are aliased by the use of "orderby", "where" and "select" keywords in the .NET Framework. So you could theoertically do the same thing above except use:



This LINQ stuff is pretty neat! It would have been particularly useful in one of my recent projects instead of writing a bunch of Stored Procedures to handle different sorts (or converting data to a dataset to sort). Anywho - we'll see how I go using this stuff now. Unfortunately, this is yet another one of those very handy at reducing code features that PHP is missing. Sure, I can code my classes to replicate those methods, but that takes time.

It's clearly evident that development time in ASP.Net is becoming substantially quicker than PHP, especially in data driven applications. With DataGrids, Classes, Direct Integration and Code Generation - you could build a Content Management System in hours (if you weren't worried about appearance) as opposed to a day or two in PHP.

Now all Microsoft have to do is speed up the processing of an ASP.Net website! (Oh, and stop using stupidly large amounts of RAM!)


Edit: Turns out TinyMCE is stripping away my < T > and < string > when no spaces are between the < and text. For the examples above, IEnumerator should read: IEnumator< T > to resemble type, and what you are trying to store shoud have a type as well. So the first example should have IEnumerator< string > and the second: IEnumerator< Item >.

[0 Comment(s)]

Five things driving me nuts!!! (23/03/2009 10:44:59 PM)

Number 5: On the weekend when I was in Frankston with Michael buying up on very cheap video games, someone asked us where the bottle shop was. Normally this wouldn't drive me nuts, but you know - we're in Frankston and some dude asked ME where a bottle shop was.

The thing that drives me nuts, is we were walking along the Nepean Hwy and well - I didn't even know where one was! WHAT GIVES!!! It's FRANKSTON!

Number 4: Modem kicking in and out - it's back on the blink, and I still haven't got my new router set up yet. You'd think I would instead of whinging about it here, but none-the-less - driving me nuts!

Number 3: ASP.Net, MySQL and Windows Server 2003 combination. Since when was Web Development so hard? Soooo many tiny bugs and it's still not done! Let's hope I can pull through on this one.

Number 2:  I haven't got a proper job, and I haven't moved out yet. I'm still at home... in the middle of no where... at 21. Once I get a full time job, and hopefully soooooooooon! I can move closer to Drouin / Warragul to make my daily commute to work easy.

NUMBER 1: I have NOT been on Public Transport since January! How could this be? I mean... I have lost my concession, but I miss it. I'm having TWS or BWS... Not the liquer store, I'm talking Train and Bus Withdrawl Symptoms... If I don't get this fixed soon... who knows what will happen.


On a slightly more interesting note about Public Transport, since I haven't been on it - my phone has racked up 84 SMS's with late/cancelled trains. Glad I'm NOT on them! Let's hope next week I get to use them... in fact, this Thursday - I might take a trip to Caulfield by Train.

[3 Comment(s)]

The difference between a good and low-end computers... (22/03/2009 09:20:40 PM)

In the last few months, it has become increasingly obvious that those buying low-end computers thinking they are high-end is becoming an epidemic. Some of you have asked for advice particularly in the last few weeks, and get quite upset when I bag out a sub-$1000 PC. This isn't because the PC is bad, but if the same thing was to apply to cars - a small Toyota Corolla no matter how suited to the task, is not as powerful as a Ford Falcon GT.

So, as a reference - here are my guidelines for what is a Budget (Low End / *crap*), Don't Bother (Mid-Range / *good*) and Awesome (High-End / *fantastic*).

Budget (Low End):

Anyone who has spent less than ~$1,000 (or ~$1,500 on a Laptop) is probably in this category. Typical traits here are:

  • 3GB of RAM or Less (i.e. 32-bit)
  • 512MB 8600GT / 9600GT / HD3650 / HD4650 or Less
  • Any AIO Motherboard where Onboard Sound / Video / LAN is used
  • Any Dual Core Processor (or Dual Core less than T7000 in Laptops)
  • Anything less than 22" Full HD LCD
  • Cheap case / PSU Combination

These computers are not bad - but it must be understood they are low-end hardware. That is; if you are the user of one of these systems, you can expect a useful life of approximately 1-2 years after which you will have upgraded at least one component for it to remain useful.

Particularly useful for those in an office situation, or someone who has low, but a regular source of income. They will (if brought today) run today's applications - but you must expect that some tweaking may be necessary.

Don't Bother (Mid-Range):

Mid-Range is probably where you will find families buying into, or those who get easily wow'd into cool technologies. These computers don't see a terribly large improvement over Budget PC's, but are obviously quicker and can last on average, an extra year longer without an upgrade.

The reason I say Don't Bother is because lets face it. You don't get that much more over a Budget PC (you may get a Quad Core, better RAM, extra FPS out of games because you have 2 Video Cards), but if you can still live with mediocre settings in games, or tweak - then you may as well tweak the entire application.

Computers in this category are:

  • 4GB of RAM
  • PC: 2 x 1024MB 8800GT / 9800GT / HD4850 / HD3850
  • Laptop: 1 x 512MB 8600GT / 9600GT / HD4650 / HD3650)
  • Dedicated Sound Card
  • Quad Core Processor (Triple Core AMD)
  • Anything between 22" - 24" LCD (Full HD)
  • Proper Case / PSU (spend at least $150 here!)

The cost is remarkably high compared to a Budget PC (i.e. ~$1,000 - ~$2,000 for a PC, ~$1,500 to ~$3,000 for a Laptop).

Awesome (High End):

Awesome computers are just that - very expensive, very powerful and will last a good 3-5 years before a real need arises to upgrade hardware (doing so is more-or-less a case of I want the newest technology). They have ridiculous specifications for you to really get the most out of a computer.

Computers in this category have:

  • +4GB of RAM
  • 2 x 1GB GTX260 / HD4870 / Other high end cards > $500 each
  • Expensive processor (such as the Core i7)
  • Dedicated Sound Card
  • Proper Case / PSU (A good $200+ here)
  • 2 x 24" LED LCD's

These are expensive. Real Expensive. Back in 2005, we had Pentium-D's and Extreme CPU's. The extreme's had 1066Mhz FSB's and the 7900GTX's were around ready for SLI. One that I know of with such a setup has no intentions of upgrading yet - and lets face it - it kick's my current PC's ass (even with it's 8GB RAM and HD4850!).


So yeah - don't go thinking your computer is awesome when it clearly isn't! My stuff certainly isn't awesome and never will be. But come on guys! There's absolutely nothing awesome about low end or mid range hardware. If it works, then good for you.

[2 Comment(s)]

Routers for a Home (05/03/2009 10:15:07 AM)

linksyswrt100rangeplusrouter_505When it comes to software and modifications to control / add functions to a router, there are plenty of packages available. Those such as DD-WRT, M0n0wall, pfsense, OpenWRT etc... are pretty neat and lightweight packages to control QoS, Route Tables etc... as well as provide Captive Portals. This sounds all well and fine, until you require a server to act as the authentication device for RADIUS, or add a web server to activate the Captive Portal. To me, this is a waste of effort if the Router is incapable of providing a low-key Captive Portal.

M0n0wal is mostly there by providing traffic shaping pipelines and inbuilt user authentication. The only thing it doesn't do in that regard is shape the amount of data or control the amount of time a user has to access the internet. More problematic is the lack of proxy filter. I suppose at the end of the day, you can't expect much from a device using merely 8MB of Storage.

The other packages do varying levels as well, such as DD-WRT not actually producing any RADIUS-like properties. I suppose this is where a niche market may be found for the home user. I suspect there are a lot of parents out there that would like to lock down the internet and restrict the time on the internet in a capacity that their children cannot tamper with (or at least - increase the difficulty). The only real way to do this is thorugh the use of a Modem / Router. Ideally, a router should be able to block keywords (usually sufficient enough), ports and dictate time / bandwidth someone has. Ideally, given the home usage market, the RADIUS Accounting packets wouldn't have to be particularly accurate, in fact - the router can probably measure at a given point in time the average packets-per-second transmitted, and count the time connected.

One possibility is to subscribe to a RADIUS server (the device is on the internet allowing access to the internet), but surely something can be done on a router for light-weight stuff. I'm not talking super-duper GUI stuff (playing with Colours and Arial should be plenty). Couple it with lighthttpd, PHP and sqllite and we may have something usable. So yeah, over the next few weeks I'm going to investigate something like this and see how I go.

[0 Comment(s)]

[Print View]