Thursday, January 31, 2013

JQuery.....



jQuery Selectors
Use our excellent jQuery Selector Tester to experiment with the different selectors.
Selector
Example
Selects
*
$("*")
All elements
$("#lastname")
The element with id="lastname"
$(".intro")
All elements with class="intro"
$(".intro.demo")
All elements with the classes "intro" and "demo"
$("p")
All p elements
$("h1,div,p")
All h1, div and p elements



$("p:first")
The first p element
$("p:last")
The last p element
$("tr:even")
All even tr elements
$("tr:odd")
All odd tr elements



$("div p:first-child")
The first p element of all div elements
$("div p:last-child")
The last p element of all div elements
$("div p:nth-child(2)")
The p element that is the second child of all div elements
$("div p:only-child")
The p element that is the only child of all div elements



$("div > p")
All p elements that are a direct child of a div element
$("div p")
All p elements that are descendants of a div element
$("div + p")
The p element that are next to each div elements
$("div ~ p")
All p elements that are siblings of a div element



$("ul li:eq(3)")
The fourth element in a list (index starts at 0)
$("ul li:gt(3)")
List elements with an index greater than 3
$("ul li:lt(3)")
List elements with an index less than 3
$("input:not(:empty)")
All input elements that are not empty



$(":header")
All header elements h1, h2 ...
$(":animated")
All animated elements
$(":focus")
The element that currently has focus
$(":contains('Hello')")
All elements which contains the text "Hello"
$("div:has(p)")
All div elements that have a p element
$(":empty")
All elements that are empty
$(":parent")
All elements that are a parent of another element
$("p:hidden")
All hidden p elements
$("table:visible")
All visible tables



$("[href]")
All elements with a href attribute
$("[href='default.htm']")
All elements with a href attribute value equal to "default.htm"
$("[href!='default.htm']")
All elements with a href attribute value not equal to "default.htm"
$("[href$='.jpg']")
All elements with a href attribute value ending with ".jpg"
$("[hreflang|='en']")
All elements with a hreflang attribute value starting with "en"
$("[name^='hello']")
All elements with a name attribute value starting with "hello"
$("[name~='hello']")
All elements with a name attribute value containing the word "hello"
$("[name*='hello']")
All elements with a name attribute value containing the string "hello"



$(":input")
All input elements
$(":text")
All input elements with type="text"
$(":password")
All input elements with type="password"
$(":radio")
All input elements with type="radio"
$(":checkbox")
All input elements with type="checkbox"
$(":submit")
All input elements with type="submit"
$(":reset")
All input elements with type="reset"
$(":button")
All input elements with type="button"
$(":image")
All input elements with type="image"
$(":file")
All input elements with type="file"
$(":enabled")
All enabled input elements
$(":disabled")
All disabled input elements
$(":selected")
All selected input elements
$(":checked")
All checked input elements


Mouse Events
Keyboard Events
Form Events
Document/Window Events
click
keypress
submit
load
dblclick
keydown
change
resize
mouseenter
keyup
focus
scroll
mouseleave

blur
unload

$("p").click(function(){
  // action goes here!!
});
jQuery Event Methods
Event methods trigger or attach a function to an event handler for the selected elements.
The following table lists all the jQuery methods used to handle events.
Method
Description
Attaches event handlers to elements
Attaches/Triggers the blur event
Attaches/Triggers the change event
Attaches/Triggers the click event
Attaches/Triggers the double click event
Attaches a handler to current, or future, specified child elements of the matching elements
Removes all event handlers added with the live() method
Attaches/Triggers the error event
The current DOM element within the event bubbling phase
Contains the optional data passed to an event method when the current executing handler is bound
Returns the element where the currently-called jQuery event handler was attached
Returns whether event.preventDefault() was called for the event object
Returns whether event.stopImmediatePropagation() was called for the event object
Returns whether event.stopPropagation() was called for the event object
Returns the namespace specified when the event was triggered
Returns the mouse position relative to the left edge of the document
Returns the mouse position relative to the top edge of the document
Prevents the default action of the event
Returns which element being entered or exited on mouse movement.
Contains the last/previous value returned by an event handler triggered by the specified event
Prevents other event handlers from being called
Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event
Returns which DOM element triggered the event
Returns the number of milliseconds since January 1, 1970, when the event is triggered
Returns which event type was triggered
Returns which keyboard key or mouse button was pressed for the event
Attaches/Triggers the focus event
Attaches an event handler to the focusin event
Attaches an event handler to the focusout event
Attaches two event handlers to the hover event
Attaches/Triggers the keydown event
Attaches/Triggers the keypress event
Attaches/Triggers the keyup event
Adds one or more event handlers to current, or future, selected elements
Attaches an event handler to the load event
Attaches/Triggers the mousedown event
Attaches/Triggers the mouseenter event
Attaches/Triggers the mouseleave event
Attaches/Triggers the mousemove event
Attaches/Triggers the mouseout event
Attaches/Triggers the mouseover event
Attaches/Triggers the mouseup event
Removes event handlers attached with the on() method
Attaches event handlers to elements
Adds one or more event handlers to selected elements. This handler can only be triggered once per element
Takes an existing function and returns a new one with a particular context
Specifies a function to execute when the DOM is fully loaded
Attaches/Triggers the resize event
Attaches/Triggers the scroll event
Attaches/Triggers the select event
Attaches/Triggers the submit event
Attaches two or more functions to toggle between for the click event
Triggers all events bound to the selected elements
Triggers all functions bound to a specified event for the selected elements
Removes an added event handler from selected elements
Removes an event handler to selected elements, now or in the future
Attaches an event handler to the unload event

Example with Callback
$("button").click(function(){
  $("p").hide("slow",function(){
    alert("The paragraph is now hidden");
  });
});

Get Content - text(), html(), and val()
Three simple, but useful, jQuery methods for DOM manipulation is:
  • text() - Sets or returns the text content of selected elements
  • html() - Sets or returns the content of selected elements (including HTML markup)
  • val() - Sets or returns the value of form fields
The following example demonstrates how to get content with the jQuery text() and html() methods:
Example
$("#btn1").click(function(){
  alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
  alert("HTML: " + $("#test").html());
});
Get Attributes - attr()
The jQuery attr() method is used to get attribute values.
The following example demonstrates how to get the value of the href attribute in a link:
Example
$("button").click(function(){
  alert($("#w3s").attr("href"));
});
Set Attributes - attr()
The jQuery attr() method is also used to set/change attribute values.
The following example demonstrates how to change (set) the value of the href attribute in a link:
Example
$("button").click(function(){
  $("#w3s").attr("href","http://www.w3schools.com/jquery");
});


The attr() method also allows you to set multiple attributes at the same time.
The following example demonstrates how to set both the href and title attributes at the same time:
Example
$("button").click(function(){
  $("#w3s").attr({
    "href" : "http://www.w3schools.com/jquery",
    "title" : "W3Schools jQuery Tutorial"
  });
});

Query width() and height() Methods
The width() method sets or returns the width of an element (includes NO padding, border, or margin).
The height() method sets or returns the height of an element (includes NO padding, border, or margin).
The following example returns the width and height of a specified <div> element:
Example
$("button").click(function(){
  var txt="";
  txt+="Width: " + $("#div1").width() + "</br>";
  txt+="Height: " + $("#div1").height();
  $("#div1").html(txt);
});
jQuery innerWidth() and innerHeight() Methods
The innerWidth() method returns the width of an element (includes padding).
The innerHeight() method returns the height of an element (includes padding).
The following example returns the inner-width/height of a specified <div> element:
Example
$("button").click(function(){
  var txt="";
  txt+="Inner width: " + $("#div1").innerWidth() + "</br>";
  txt+="Inner height: " + $("#div1").innerHeight();
  $("#div1").html(txt);
});


jQuery outerWidth() and outerHeight() Methods
The outerWidth() method returns the width of an element (includes padding and border).
The outerHeight() method returns the height of an element (includes padding and border).
The following example returns the outer-width/height of a specified <div> element:
Example
$("button").click(function(){
  var txt="";
  txt+="Outer width: " + $("#div1").outerWidth() + "</br>";
  txt+="Outer height: " + $("#div1").outerHeight();
  $("#div1").html(txt);
});
The outerWidth(true) method returns the width of an element (includes padding, border, and margin).
The outerHeight(true) method returns the height of an element (includes padding, border, and margin).
Example
$("button").click(function(){
  var txt="";
  txt+="Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
  txt+="Outer height (+margin): " + $("#div1").outerHeight(true);
  $("#div1").html(txt);
});




jQuery More width() and height()
The following example returns the width and height of the document (the HTML document) and window (the browser viewport):
Example
$("button").click(function(){
  var txt="";
  txt+="Document width/height: " + $(document).width();
  txt+="x" + $(document).height() + "\n";
  txt+="Window width/height: " + $(window).width();
  txt+="x" + $(window).height();
  alert(txt);
});


The following example sets the width and height of a specified <div> element:
Example
$("button").click(function(){
  $("#div1").width(500).height(500);
});

jQuery HTML / CSS Methods
The following table lists all the methods used to manipulate the HTML and CSS.
The methods below work for both HTML and XML documents. Exception: the html() method.
Method
Description
Adds one or more class names to selected elements
Inserts content after selected elements
Inserts content at the end of selected elements
Inserts HTML elements at the end of selected elements
Sets or returns attributes/values of selected elements
Inserts content before selected elements
Makes a copy of selected elements
Sets or returns one or more style properties for selected elements
Removes selected elements (keeps data and events)
Removes all child nodes and content from selected elements
Checks if any of the selected elements have a specified class name
Sets or returns the height of selected elements
Sets or returns the content of selected elements
Returns the height of an element (includes padding, but not border)
Returns the width of an element (includes padding, but not border)
Inserts HTML elements after selected elements
Inserts HTML elements before selected elements
Sets or returns the offset coordinates for selected elements (relative to the document)
Returns the first positioned parent element
Returns the height of an element (includes padding and border)
Returns the width of an element (includes padding and border)
Returns the position (relative to the parent element) of an element
Inserts content at the beginning of selected elements
Inserts HTML elements at the beginning of selected elements
Sets or returns properties/values of selected elements
Removes the selected elements (including data and events)
Removes one or more attributes from selected elements
Removes one or more classes from selected elements
Removes a property set by the prop() method
Replaces selected elements with new HTML elements
Replaces selected elements with new content
Sets or returns the horizontal scrollbar position of selected elements
Sets or returns the vertical scrollbar position of selected elements
Sets or returns the text content of selected elements
Toggles between adding/removing one or more classes from selected elements
Removes the parent element of the selected elements
Sets or returns the value attribute of the selected elements (for form elements)
Sets or returns the width of selected elements
Wraps HTML element(s) around each selected element
Wraps HTML element(s) around all selected elements
Wraps HTML element(s) around the content of each selected element

Wednesday, January 30, 2013

Some Query....

 Table :emp

eid    Name    salary
1    ravikb       2000
2    gorav        3000
3    sorav        2000
4    rohit          2500
5    hemat       4500
6    sachin      6230
7    suraj       4054
8    sttt          8000
13    sttt        1000
14    fr           2010

 

1.find second highest salary

select min(salary) salary from emp where salary in(select top 2 salary from emp order by salary desc)

OUTPUT : 

salary

6230 


2. Find  TOP 3 highest salary Without Top Function

 CASE 1.

Select * from emp e1 where 2>=(select count(salary) from emp e2 where e1.salary<e2.salary)
order by salary asc

CASE 2.

With CTE as
(
Select eid,Name,salary ,ROW_NUMBER()over(order by salary desc)r from emp
)

select Salary from CTE where r<=3

OUTPUT :

8    sttt        8000
6    sachin    6230
5    hemat    4500

 

3. Find  TOP 2 Lowest salary Without Top Function

With CTE as
(
Select eid,Name,salary ,ROW_NUMBER()over(order by salary asc)r from emp
)

select Distinct Salary from CTE where r<=2

 

OUTPUT :

1000
2000

 

Wednesday, December 7, 2011

sql query

find duplicate record


select NAME, count(*)dups
from SHERA
group by NAME
having count(*) > 1
------------------------------

Delete duplicate record

with tt(Name,dups) --CTE
as
(
select NAME, ROW_NUMBER() over(partition by Name order by NAME) dups
from SHERA
)
delete from tt where dups>1


Thursday, November 24, 2011

Anonymous types



The Basics


Anonymous types are those types which are not declared before they are used. Say while you are doing your program, you want a temporary storage of your data, what you need to do, you need either declare a concrete class for the storage of the same, or you can use any collection like ArrayList, HashTable etc to store the key value collection. C# 3.5 and onwards allows you to dynamically create a type and use it directly in your program. You can use ‘var’ or implicit type declaration to ensure you could use the properties just like normal types.
var myruntimeObject = new { FirstProperty = 10, SecondProperty = new DateTime(), ThirdProperty = "string type" };

Console.WriteLine("Type of myruntimeObject is {0}", myruntimeObject.GetType().Name);
Console.WriteLine("Type of FirstProperty is {0}",       myruntimeObject.FirstProperty.GetType().Name);
Console.WriteLine("Type of SecondProperty is {0}", myruntimeObject.SecondProperty.GetType().Name);
Console.WriteLine("Type of ThirdProperty is {0}",        myruntimeObject.ThirdProperty.GetType().Name);

Console.Read();

In the above example I have created one anonymous type using var. The var will give you a chance to get intellesense in your code. You can see the variable holds an object of an anonymous type generated runtime. The power of var in code can also be tested. Just hover over the var keyword and you will see that the type is defined as anonymous.


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5yRgZkchYPQKIrmZjZ7WM2WO-firKVIbOrsbH47HsUEqsY9otsXAAwqt1o2AibrZT25DGgtSAdx2EVVAPwR9b5HSJqAQUcWVnvdCcXgjyrylrbadOOYz7iclnp4MwOf78P5IplWhXQGo/s640/1.JPG
In the previous code after the declaration of anonymous object, I have tried to see what exactly the type of the object and the members look like that I have declared. Lets see the output below :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifwVRBfuU4rXaX8IbFJB3w3gNercTQ7xjqD7kHmbXlt7PQjwHqOaThluLje6eGqiwNZCZwlwj9Ql8Zg0VDNyiD2uLVSvMs1_xG8xOKcezhPBZlkGXAOxAsWzRNrZOFRUYCyiMNpOUzvlk/s1600/2.JPG

So the actual type created by CLR at runtime looks like <>f_AnnonymousType0`3. We will discuss about it later in this post, but the thing that you might wonder, is the compiler is smart enough to declare the type of the members exactly. The FirstProperty is declared as Int32, second as DateTime and third as String. The type is self evaluated in the code and declared accordingly.

Now if I change the value just a bit like :
var myruntimeObject = new
{
   FirstProperty = 10d,
   SecondProperty = new DateTime(),
   ThirdProperty = "string type"
};

The code says the FirstProperty is double. The 10d is evaluated as double value.

You are also free to declare anonymous type for its members.
var myruntimeObject = new { FirstProperty = 10, SecondProperty = new DateTime(), ThirdProperty = "string type", MoreProperty = new { N1 = "" } };

Here the MoreProperty declares another anonymous type implicitly. But there is restriction the usage of anonymous types. You cannot declare a method inside any object declaration, even there is no way to give accessibility specifier to its members. All the members inside an anonymous type are readonly properties, hence if you try to do :
myruntimeObject.FirstProperty = 20;

It will prompt you an error.

Another important thing about anonymous type is that the compiler is smart enough to use anonymous type gracefully. It does not create a new anonymous type if the next instruction is exactly tries to create the same class. Hence if you use :
var myruntimeObject = new { FirstProperty = 10, SecondProperty = new DateTime(), ThirdProperty = "string type", MoreProperty = new { N1 = "" } };

var myruntimeObject2 = new { FirstProperty = 30, SecondProperty = new DateTime(), ThirdProperty = "string type2", MoreProperty = new { N1 = "blah blah" } };

Both internally represent the same type.

The Internals

Now as you know the basics of anonymous Types, lets look deep into its internal structure. In terms of IL, both var and anonymous type is nothing. Var is actually represented as actual type while anonymous type is mapped to an actual type.

The object creation looks similar to
<>f__AnonymousType0<<N1>j__TPar> moreproperty = new <>f__AnonymousType0<<N1>j__TPar>("");

<>f__AnonymousType1<<FirstProperty>j__TPar, <SecondProperty>j__TPar, <ThirdProperty>j__TPar, <MoreProperty>j__TPar>
                myruntimeObject = new
                <>f__AnonymousType1<<FirstProperty>j__TPar, <SecondProperty>j__TPar, <ThirdProperty>j__TPar, <MoreProperty>j__TPar>(10, new DateTime(), "string type", moreproperty);

Hence it seems to be quite big name for the type and also includes a generic argument named <N1>j__TPar. This lets the object to refer the same type when argument differs. As Reflection API is not good enough to show the actual generic types, it shows a numeric representation for the same.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjW51iE36T0FRSSTVs4r6WMz22iyXO4f5KnxHplVQrVA2OUZurdMWjadKeHgNn2Bc9UzM8AGev_vejlhVk04WYlM4JkPQeF8ddoQpMmpXXFMAFlonKl1XAg31msbxg8-IlSDsOH8HSE2AQ/s1600/3.JPG


Now as you can see, the Compiler generated type for MoreProperty looks like one above. The first thing that you should notice is the DebuggerDisplayAttribute just above class. With this attribute, Visual Studio debugger shows only <anonymous type> rather than the whole type name.

The Type takes a Generic type argument <N1>j__TPar in its constructor and builds the class. You should notice the parameter is marked as Readonly, so it is not writable. Also the type overrides ToString, GetHashCode and Equals by using the generic EqualityComparer. This looks a nice implementation.

Conclusion

Finally, to conclude, I must say anonymous types is an interesting feature which is widely used and eventually changed the way of coding. It is very much useful while working with LINQ. We will discuss internals of LINQ in later part of the series. But there should be a provision to define methods inside an anonymous type, which I think will be added sooner than later.


------------------------------------------------------------------------------------------------------------------------------

LINQ means Language Integrated Query is one of the major step forward to .NET framework to support queries to work on objects. LINQ allows you to write custom query statements on .NET objects or more specifically any IEnumerables to filter or fetch data from it.



Simple Projection

To start with Internals, let me create a List of strings on a Type. You should already know, that you can easily replace .NET initializer to allow initialize array into any IEnumerable.
public static List<string> myList = new List<string>
{
"Abhishek",
"Amit",
"Abhijit",
"Kunal",
"Krishna",
"Dhananjay"
};

Yes it will compile, and by your previous knowledge, you think it is initialized the same way if you use an array. But it isnt.

C# compiler automatically replaces the code with proper List.Add statements to add each individual statements. If you see the same code in reflector, you will see a new backup list variable is added to eventually add each strings internally.
var simpleprojection = from i in Program.myList
                                   select i;

Now starting what we call the most basic LINQ query represents to project each individual elements in the list directly into another IEnumerable. Now how does the IL looks like :
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSwSOGvRsRoM1GR_MO9S5WTv4sn86nWQGQs1s-ZCbkiUkEqhEz8rNnUCVU3D1J3FXxFUCreB5c6YKl0oi-wZINGmwPIFGAewVZlMhLxo8VNCNoNXK8raL42-WU0PJ-LTW_G9me-CxvWhA/s1600/l1.JPG


Now here you wonder how both the sections as I have discussed looks like. Yes, the initializer introduces a Static constructor internally (created by compiler itself) which adds up each of the strings into the List.  On the other hand, simpleprojection gets an Enumerable from Enumerable.Select which takes an IEnumerable and a delegate.

Internally the Enumerable puts each element from an enumerator and process it using the delegate we pass. Just as our delegate actually returns i. So nothing will be changed. Now let me change the code slightly.
var simpleprojection = from i in Program.myList
                       select new { x = i.StartsWith("A"), y = i.Reverse() };

Hmm, the code looks similar, but here we have yield a new object. Now if you see the IL, it produces a concrete implementation of the class to hold X and Y and eventually creates object of each.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmCwaAbVkmnPaxDbvKatza_YRWnBISZ4t5ZX3GfkuhH2YkOwStraompZ9BXknxVvYSCmYlIFylM94KaMqsgvOrRAWWXuzAXTf1h1Enr-eKBzwmvw-y9pdxrEee3hGCAQ3wtf5mZlLENy0/s640/l2.JPG
Click to see the Image
Well, yes,  as you can see, the IEnumerable returns an object of Concrete Type. Know more about anonymous types from "Internals of Anonymous Type".

Now coming to our scenarios, the code is same as the other one, just replacing the select statement inside the delegate.

Lets add up a bit more with Let :
var simpleprojection = from i in Program.myList
                        let isStartWithA = i.StartsWith("A")
                        let reverseString = i.Reverse()
                        select new { x = isStartWithA , y = reverseString };

Well, the code works the same with two let statements which assigns the value of each on separate variable isStartWithA and reverseString. But internally everything is now changed.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaup8DF4yKssDVs34GqPFaOT3SlPy_NzgJTucaUGE11v6rOE2B_BWMPJRDjQmX9Gg54dohBDczgXdBsvxibab7RNGqqhdI-5vIy0WTe31zed4Bb_ayL6oNbmpA01MR8vvZyQdcZcQDtLM/s1600/l3.JPG

Hmm, the statement is now replaced with three Enumeration.Select calls nested one within another. Lets break these lines manually to understand what is happening.
var  firstEnumerable = Enumerable.Select(myList, delegate (string i) {
                                                    return new { i = i, isStartWithA = i.StartsWith("A") };
          });
          //Replacing <>f__AnonymousType0<string, bool> fType<string, bool>
          var secondEnumerable = Enumerable.Select(
                                            firstEnumerable,
                                            delegate (fType<string, bool> obj) {
                                                return new { Fobj = obj,
                                                                reverseString = obj.i.Reverse<char>() };
                                            });

        //Replacing <>f__AnonymousType1<<>f__AnonymousType0<string, bool> to fType2<fType<string,bool>, IEnumerable<char>>
         var simpleprojection = Enumerable.Select(
                                   secondEnumerable,
                                    delegate (fType2<fType<string,bool>, IEnumerable<char>> obj) {
                                    return new { x = obj.Fobj.isStartWithA,
                                                y = obj.reverseString };
                                });

I have intentionally replaced few compiler generated typenames to some other to make you understand code better.

In the first call we pass the list to a delegate which creates a object with variable isStartWith which is the boolean representation of our first let statement.

In the second call, we pass the result of first call. The second delegate generates the reverseString wrapping around the first call object.

Finally, in the outermost call, it will generate the end product.

Now why should you need three nested calls ? Yes there is a reason. Actually when you define the first let statement, you would internally think that from the next statement we should make the variable x available. So the scope of the First let, isStartWith lies on both of the next two lines reverseString and select. Hence we pass on the value in a anonymous type. Thus we can easily use isStartWith on both of these lines.

Similarly on the next successive calls we need to wrap around all the let statements internally into its respective anonymous types until finally we reach out the select. The compiler is smart enough to go on create anonymous types with first property being the parent object and second being the actual let statement.

Hence you can infer, for every let statement in LINQ query, compiler will generate 1 anonymous type at least.

Now lets alter our query to introduce Order By.
var simpleprojection = from i in Program.myList
                        orderby i
                        select i;

If we look into the Reflector for the code we see the code writes the same thing as the first code, the only difference is it is using Enumerable.OrderBy instead of Select and returns back IOrderedEnumerable. The OrderBy actually processes each element using the delegate that we pass, and puts it into a call to OrderedEnumerable, which internally uses EnumerableSorter to get each object from the List.

If you ask me does it internally stores the elements in sorted order after it is being processed, then I think you need to read more about IEnumerable may be from C# iterators. Actually when you enumerate from IOrderedEnumerable, it will internally get the next element from the list which represents the ordering. As IEnumerable internally maintains a state machine, it will save its state until further yield.

Next, lets put a GroupBy.
var simpleGroup = from i in Program.myList
                        group i by i.Count() into g
                        select new { Count = g.Count(), Elements = g };

Well, here we grouped the list based on character count. You should note each Group statement gives you an object (g) which represents the group and you can use g in your select statement.

Now lets see it into reflector:
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgarOa0YKHS_4LEOUOTyUells6MxesE4ipyPkDSeM3GkmSfXnR5gToBIo2Pgb98hIU3Mhk2hkI-MvYHxHdS_GJjBzs9u8v1AMkmur-dxtgmYLy4yCHcaTEtg1GR2Eh-xa8Ep4OcyBiesW8/s1600/l4.JPG

So you can see here in to code, the C# compiler wraps around a Enumerable.GroupBy inside the Enumerable.Select where the former returns an IEnumerable of IGrouping members. The select statement then uses this IGrouping object to generate its result.

Similar to OrderBy the GroupBy uses GroupedEnumerable object to generate each Group object on each yield. You should note, the object g is internally managed IGrouping object, hence groups does not generate additional Types.

Finally lets see how Join statement looks like. For simplicity I used inner join statement.
var customers = new List<Customer>{ new Customer { Id = 1, Name="Abhishek"},
                                new Customer { Id = 2, Name = "Amit"},
                                new Customer { Id = 3, Name = "Abhijit"},
                                new Customer { Id= 4, Name="Kunal"},
                                new Customer { Id = 5, Name= "Krishna"},
                                new Customer { Id = 6, Name = "Dhananjay"}
};

var orders = new List<Orders>
{
    new Orders { Id = 1, CustomerId = 4, NosOrder=20},
    new Orders { Id = 2, CustomerId = 1, NosOrder=30},
    new Orders { Id = 3, CustomerId = 2, NosOrder=50},
    new Orders { Id = 4, CustomerId = 1, NosOrder=10},
    new Orders { Id = 5, CustomerId = 3, NosOrder=60},
    new Orders { Id = 6, CustomerId = 4, NosOrder=10},
};

var simpleJoin = from c in customers
                join o in orders on c.Id equals o.CustomerId
                select new { c.Name, o.NosOrder };

Well, here I have put one of the most simplest customer and order Join which puts only the equal elements on the result. If you see this on reflector
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJC7myOES3VhAb8FfYQP8Wju_zrCpIhlmM_FKFjvTpwyYDS5OgqqAOSLzYWZmXm1RkO6e7zpJl5qh1qqLA0AmWwFe_6-taVHbfRcoBn3DVZilsxkWo8xyfmhZP7JHHOA89I0P32DkSDZE/s1600/l5.JPG


Hmm, it eventually calls upon the Enumerable.Join statement with delegates which filters the first argument, the  second argument and the result. In case of Joins, for each yield, the first and second delegate is fetched once, and 3rd delegate is called upon for each successful match in the lists. The framework uses JoinIterator internally to iterate between two lists.

For OuterJoins :
var simpleJoin = from c in customers
                join o in orders on c.Id equals o.CustomerId into outer
                from l in outer.DefaultIfEmpty()
                select new { c.Name, l.NosOrder };

It introduces Enumerable.GroupJoin nested inside Enumerable.SelectMany.

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqqwliT9xqJ2lbWR5cA-iYrfUB3v9h4Q7FMd33yvEoi8XQ_lydAEE7SZ-JdvQSBCr-qRJmxWC8POGJyyawixXzmRYsYpJ47JWCQhyNI8YDTOcj1-NRYnZ-YPd-uqebsaXoMJ6yKQLQXgg/s1600/l6.JPG

The GroupJoin uses GroupJoinIterator to group each individual first delegate with second delegate and each group is selected.



by abhishek sur