Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Friends,
Well yesterday I was appearing for an interview when this Question was asked to me for which I was confused to answer.

Question is:

We write query statements in LINQ as:
Consider 'db' as an object of a data context class.
C#
var q =from c in db.Customers select c;


So why do we write the query starting as "from c in ......." instead of simply writing as
C#
Select * from db.Customers


I also answered the interviewer that this is the Syntax to write.But he asked me to answer in more depth.
Posted
Comments
Nelek 2-Nov-12 14:38pm    
And the question is.... ?

LINQ is made to write SQL queries easier.
For simple queries, like your query, LINQ makes no difference, but if the SQL queries are longer, it's easier to use LINQ.
Have a look here:
http://www.linqpad.net/WhyLINQBeatsSQL.aspx[^]
 
Share this answer
 
v2
Comments
P_Dash 2-Nov-12 14:56pm    
Sir, I know it's easy to write complex sql queries in LINQ, but for this why do we start the query with 'from' ?

U've provided a good link, but plz explain me this one, coz i need to answer that interviewer tomorrow.

Thanks
i believe "select * from " is the syntax that database understands.

linq is intended to have the flexibility to query the objects similar to database style.

but select "*" will not be understood by linq. in the above query, "from c" ==> from customer object in the list of db.customers select one customer.
may be this should be a query with more filters on it to select a specific customer of certain criteria.
 
Share this answer
 
Comments
P_Dash 2-Nov-12 14:46pm    
Well I know it's a Simple query.Since my question isn't anything related to how to write query, I used just a simple query.

I just want to know why r we starting the LINQ queries with 'from' ???
fjdiewornncalwe 2-Nov-12 14:58pm    
LINQ queries start with "from" because they do. That is how the syntax was designed. It's like asking "Why do sql select queries start with select?"
P_Dash 3-Nov-12 4:36am    
Dear, don't get angry.
This is the question asked to me.
When I replied as u...Interviewer said answer me more accurate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900