>Query keywords – from

>

<!–
/* Font Definitions */
@font-face
{font-family:”Cambria Math”;
panose-1:2 4 5 3 5 4 6 3 2 4;
mso-font-charset:162;
mso-generic-font-family:roman;
mso-font-pitch:variable;
mso-font-signature:-1610611985 1107304683 0 0 159 0;}
@font-face
{font-family:calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;
mso-font-charset:162;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:-1610611985 1073750139 0 0 159 0;}
@font-face
{font-family:verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;
mso-font-charset:162;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:536871559 0 0 0 415 0;}
@font-face
{font-family:consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;
mso-font-charset:162;
mso-generic-font-family:modern;
mso-font-pitch:fixed;
mso-font-signature:-1610611985 1073750091 0 0 159 0;}
/* Style Definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-unhide:no;
mso-style-qformat:yes;
mso-style-parent:””;
margin-top:0cm;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:”Calibri”,”sans-serif”;
mso-ascii-font-family:calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:calibri;
mso-fareast-theme-font:minor-latin;
mso-hansi-font-family:calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:”Times New Roman”;
mso-bidi-theme-font:minor-bidi;
mso-fareast-language:en-us;}
.msochpdefault
{mso-style-type:export-only;
mso-default-props:yes;
mso-ascii-font-family:calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:calibri;
mso-fareast-theme-font:minor-latin;
mso-hansi-font-family:calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:”Times New Roman”;
mso-bidi-theme-font:minor-bidi;
mso-fareast-language:en-us;}
.msopapdefault
{mso-style-type:export-only;
margin-bottom:10.0pt;
line-height:115%;}
@page section1
{size:595.3pt 841.9pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;
mso-header-margin:35.4pt;
mso-footer-margin:35.4pt;
mso-paper-source:0;}
div.section1
{page:section1;}
–>

Let’s start to check each query keyword with samples.

from: Is like the “from” in SQL, refers to the source.

from c in Customers
from
n in numbersArray
from
f in Directory.GetFiles(“c:\\temp”)
from
p in Diagnostics.Process.GetProcesses()
from
di in DriveInfo.GetDrives()

In these incomplete samples c, n, f, p, di  are variables referring to an object of type it is used with. For example if Customers is a collection of Customer objects then “c” in “from c in Customers” refer to a Customer type. In SQL, you may think of it as the local alias of table like:

Select c.CustomerID, c.CompanyName, c.ContactName from Customers c

In method syntax as far as I know there isn’t something to use instead (you have it as a parameter – this Ienumerable<Tsource> source). The reason that you first write “from” is intellisense support. That makes sense if you think how we write an SQL select. Think that you have 50 columns in a table and you want to select 10 of them, visualize the ideal way that you could write it. We all have some ways to make the life easier fro ourselvees in writing those type of queries but none of them would be as good as getting a direct support from the IDE itself. Saying it like:

from Customers c Select c.CustomerID, c.CompanyName, c.ContactName

we would be getting help from intellisense when writing c.CustomerID, c.CompanyName, c.ContactName. I think it was a syntax choice (probably to match natural English) to write it as:

from c in Customers

instead of

from Customers c

Unlike SQL where there is a single “from” clause in Linq you can have multiple. Multiple “from” create a type of join and there is also “join” keyword. Here are samples for multiple “from”:

from c in Customers from o in c.Orders

is an implicit join (equijoin, inner join) – in case of Linq to SQL and Linq to Entities it infers the join as c.CustomerId = o.CustomerId). If you make a typo (might be intentional but generally a newbie typo) and write it as:

from c in Customers from o in Orders

then it is still an implicit join (non equijoin, cross join) but selects all records from both tables and match each producing cartesian result – same as if we do a typo in SQL version like:

select * from Customers, Orders 

leaving out the join (either with a join clause or where specifying the relation) ends in a cross join. It might be intentional, say to create a calendar like:

2007,January
2007,February

2009,January
2009,February

int[] years = {2007,2008,2009};
string[] months = {"January","February","March",
                  
"April","May","June",
                  
"July","August","September",
                  
"October","November","December"};
var myCalendarHeaders =
        
from y in years
        
from m in months
        
select String.Format("{0},{1}", y,m);
myCalendarHeaders.Dump();

About cetinbasoz

A developer working primarily with C#,F#,VFP,SQL server, Silverlight,Linq ...
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s