Showing posts from February, 2012

Regular Expression in LINQ

string[] companies = { "microsoft", "IBM", "oracle", "google", "apple", "facebook", "RIM" };
//wildcard search to "%o%o%"
Regex regex = new Regex("oo");
var oo = companies.Where(c => Regex.IsMatch(c, "o.*o")).Select(c => c);
var oo1 = from c in companies
where (Regex.IsMatch(c, "o.*o"))
select c;

Here we are searching for company names containing two “o”, need not be consecutive.

How to use LIKE, IN - in LINQ

How do we use LIKE (from SQL) in LINQ?

One option:
-> use string.Contains() methodstring[] companies = { "microsoft", "IBM", "oracle", "google", "apple", "facebook" };
//get companies which have 'oo' in them
IEnumerable<string> companiesWithAnO = (from c in companies
where c.Contains("oo")
select c).ToArray();

Also we can use string.StartsWith() and EndsWith().

-> Second option: This is only if we are using LINQ To SQL.string[] companies = { "microsoft", "IBM", "oracle", "google", "apple", "facebook" };
//get companies which have 'oo' in them
IEnumerable<string> companiesWithAnO = from c in companies
where SqlMethods.Like(c,"%oo%")
select c;

The IN operator is also quite easy.string orangeFruit = "orange";
string[] fruitBasket = {"apple","orang…

LINQ: Differed Queries

LINQ queries, by default, are differed. It means that the original source collection is accessed only when the collection containing the LINQ output is enumerated. Let us see an example.
string[] companies = { "microsoft", "IBM", "oracle", "google", "apple", "facebook" };//get companies which have 'oo' in themIEnumerable<string> companiesWithAnO = from c in companies where c.Contains("oo") select c;foreach (var item in companiesWithAnO){ Console.WriteLine(item);}
Output: google, facebook.

Let’s add a new line after the LINQ query.
try{string[] companies = { "microsoft", "IBM", "oracle", "google", "apple", "facebook" };//get companies which have 'oo' in them IEnumerable<string> companiesWithAnO = from c in companies where c.Contains("oo") select c;//set IBM string to null companies[5] = nul…

LINQ: Extension Methods for Console apps

When designing Console apps for Windows, we usually write a lot of message to the console. It is a pain to write “Console.WriteLine(“xxxxxx”)” every time.So let’s use extension methods to ease the pain.Step 1> Create a Library project, add a class:

namespace LINQ.ExtensionMethods
publicstaticclass MExtensions
publicstaticvoid Write(thisstring s)

Step 2> Create a Console project, in the Main method:

staticvoid Main(string[] args)
"Welcome to Console project".Write();

Let’s do this for a collection, no more writing loops or calling a method to iterate. Here we write an extension method for IEnumerable<T> in the class MExtensions above.publicstaticvoid Write<T>(this IEnumerable<T> c)
foreach (var item in c)
And to call:string[] games = { "Soccer", …

How to check when a machine was restarted?

Yesterday evening one of our test servers automatically rebooted. And today morning we found out that the last night’s test run was interrupted. Checking the logs and stuff, found out this was the case. So took on the task to find out what happened.The best friend is Windows Event Logs – it is a reliable source to check computer’s history. (What happens in case of hibernate? #to-find)Shutdown or reboot could happen because of various reasons and for each reason we could get a different Event ID. So we need is a quick way to find out the boot time . There are few events we can trust, the events logged by Event Logging service – that it is starting or shutting down. Very rarely would some one disable this service or start/stop it. The logs logged by Event Log Service are:6005: logged at boot time, when the service starts
6006: at shutdown, that the service is stopping

The Date/Time of entries with these IDs can be taken as a rough idea when the computer stopped and booted.In our case, we …