Mar282008

LINQ queries are deferred :)

Ok, probably we all heard that linq queries are deferred, so be careful bla bla.

Why is it deferred ? i think for performance, and resource use. By what makes linq queries less resource dependent ?
Let's have a quick look at the Where extension method.

[code:c#]
public static IEnumerable<source>Where<source>(this IEnumerable<source> sourcelist, Func<source>, Boolen>predicate)
{
    foreach(source element in sourcelist)
    {
         if(predicate(element) yield return element;
    }
}
[/code] 

We can see that Where is an extension method (the first parameter is marked with "this" and it is a static function) of any IEnumerable object. 
What basically it does is to iterate through each element in the collection (IEnumerable collection), calling the function predicate for each element,
and if the element returns true in the predicate function; return the element. But it is smart enough not to return all the elements in once,
in stead it uses yield. yield retuns the current element and waits for the next call to return the next element, this makes it more effecient.
Also where method returns an IEnumerable object which means the result can be appended to Select, Where etc etc.

 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Mar212008

Resharper is GREAT

Since i installed resharper ( few days ago ), i enjoy using vs 2008 more. Not only it helps me to write fast code, it also teaches me better coding :)

You wonder how ? When i have a data representing class, i always overload ==,!=, Equals, GetHashCode() functions, such as:

[code:c#]

    public override string ToString()
        {
            return Name;
        }
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            if(obj == null)
                return false;
            Branch temp = (Branch) obj;
            return temp.Name.ToLowerInvariant().Equals(this.Name.ToLowerInvariant());
        }

        public static bool operator==(Branch d1, Branch d2)
        {
            object o1 = (object) d1;
            object o2 = (object) d2;
            if( (o1== null) ||(o2==null))
                return false;

            return d1.Equals(d2);
        }

        public static bool operator !=(Branch d1, Branch d2)
        {
            return !(d1 == d2);
        }

[/code] 

Today while i was doing the same thing for another class, right before i was going to overload, resharper's yellow icon poped up, i hit insert enter, and resharper asked me if i want to overload members, i click sure go ahead, here is the resharper code which is a lot smarter than mine:

[code:c#]

public bool Equals(Counselor counselor)
        {
            if (counselor == null) return false;
            return Equals(Email, counselor.Email);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as Counselor);
        }

        public override int GetHashCode()
        {
            return Email != null ? Email.GetHashCode() : 0;
        }

 

[/code] 



Tags: ,

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Mar202008

Twitter

I signed up for this mini blogging site with one of my friend's invitation. At first i said, why i am gonna use this at all.

Now i find it fun, and usefull :). u can track who/where/what/when/whom/how kinda things 

Be there, add me : volkanuzun 

http://www.twitter.com 



Tags: , ,

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Mar182008

Visual Studio 2008, Sql Server 2008 Launch Event

We've finished our launch event, We were the first in southern california i guess :). I think it was pretty good, there are some stuff that we have to work on, but we ll get better i promise :)

You can see some of the photos here : http://www.flickr.com/photos/iedotnetug/

James summarized the event as :


George Ande – ASP.NET Ajax with .NET 3.5
Sean Dorsett – Visual Studio 2008 Tips and Tricks
Volkan Uzun – New features of C# 3.0
Steve O’Brien – LINQ
Matt Penner – GIS and Spatial Analysis in SQL 2008
Cigdem Uzun – Silverlight 2.0
 
While the attendance was not as much as I had thought, based on the VS 2005 Launch at ITT Technical Institute (overloaded with tech students), here are some details:
 
RSVP’s:  36
Actual Attendance: 34 (a IEDOTNETUG Record!)
Non RSVP Attendees: 8
First Time Attendees: 11 (each one came to me, stating they would be back)
Total Raffle Ticket Sales: 364 (oh my!)
Number of software licenses (VSTS 2008, SQL 2008, WS 2008, telerik, CodeSmith, ReSharper, Infragistics) won: 38
Number of books (O’Reilly, Wrox, Pearson) won: 22 (jeez, I just got those too)
Number of job offers mentioned after break: 4
Pizza slices left over: 1



Tags: ,

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Mar162008

operator== overloading and null check problem

For a project i am working on, i wrote a WebSite class, which will hold information about a web site. In the database the web sites should be unique, and by business rules uniquness is defined as, no same name web site shall exist twice, no same urls shall exist twice; so in my website class i overloaded == sign to check for these two properties such as :

[code:c#]

public static  bool operator == (WebSite site1,WebSite site2)
{
   return site1.Equals(site2);
}

public override bool Equals(object obj)
{
    WebSite temp = (WebSite)obj;
    if(this.Name==temp.Name) return true;
    if(this.Url==temp.Url) return true;
    return false;

}
[/code] 

At the first glance, the code seemed right to me; however what is WebSite2 is null, so we should check it right ? i changed the code as below:

[code:csharp]

public override bool Equals(object obj)
{
    WebSite temp = (WebSite)obj;
    if(temp == null) return false;
   if(this.Name==temp.Name) return true;
    if(this.Url==temp.Url) return true;
    return false;

}

[/code]

and... my code broke. Himm where was the problem ? there is a recursive call in the null check :) so i  have to typecast upto object before  i do the null check, or before typecasting it to website i have to do the null check such as :

[code:csharp]

public override bool Equals(object obj)
{
    if(obj ==null)return false;
    WebSite temp = (WebSite)obj;
     if(this.Name==temp.Name) return true;
    if(this.Url==temp.Url) return true;
    return false;

}


[/code] 

By the way i catch this error with unit testing :)) 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Mar142008

OrderBy.OrderBy... (LINQ)

I was suprised to findout that u cant pass the returned resutl from orderby to another orderby in Linq.

So u can't say : myresult.OrderBy(s=>s.LastName).OrderBy(s=>s.FirstName)...
OrderBy returns a IOrderedEnumerable<T>, and takes a IEnumrable<T> as input and these two are incompatible :).

You have to use .ThenBy clause. what a heck!!!



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Mar092008

A few notes about linq for myself :)

  • If a deferred operator has a return type of IEnumerator<T> or IOrderedEnumerator<T> , it is deferred.
  • Func Delegates, the first parameters is always the return type and can take upto 4 params. such as : Func<int,bool>GreaterThanTwo = i=>i>2
  • For nongeneric C# collections use Cast or OfType
  • If there is an error in the query for the deferred operations, error may be thrown way after the query call


Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Mar022008

SecureIT

From Moday 4 to Monday 6, i will be at a conference "SecureIT" in san diego.

You can find more information : http://www.secureitconf.com. This is the first time i am attending this conference which is held almost once a year and every year :). I will post about the coference, the meetings, will try to pass the information. 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses