Tuesday, November 20, 2012

Compare CAML and LINQ

Yesterday - I interviewed couple of candidates for a new lead developer position in my project. Unfortunately none of those candidates were able to compare CAML and LINQ properly which is a basic question. Let me share my notes here

CAML - Collaborative Application Markup Language is an XML based markup language that helps developers to both construct and display data. CAML can be used by  developers to query against SharePoint lists and views, when programming against the SharePoint API. CAML is also supported by SharePoint Web Services.

LINQ - Language-Integrated Query is relatively new feature that extends powerful query capabilities to the language syntax of C# and Visual Basic. It introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store including SharePoint Lists. The LINQ to SharePoint Provider is defined in the Microsoft.SharePoint.Linq namespace. It translates LINQ queries into Collaborative Application Markup Language (CAML) queries. It is no longer necessary for developers to know how to write CAML queries. LINQ queries can be used in server code.  

Disadvantages of CAML: 
  • CAML query is text based so, if we are joining two lists across a lookup field there may be various problems associated with that. 
  • There is no mechanism to know until run time if the query is written correctly or not. If the query is not correct, then it will simply fail at run time. Means it won't support at design time 
  • When writing the query, you have no idea what CAML elements are legal in the syntax without having a reference open. 
  • The query is somewhat difficult to understand. We cannot determine easily what the query is doing and what lists are being joined. 
  • The data returned from the query is placed in a SPListItem collection, which does not provide strongly typed business entities.
Advantages of LINQ over CAML: 
  • First advantage is, it is an object-oriented query language. 
  • It can provide strongly typed objects at design time; we can create queries in code and can check that they are correct because we can the compiles the code. 
  • The results are returned from queries are strongly typed objects, so the items and fields can provide compile-time checking. 
Disadvantages of Using LINQ 
  • LINQ translates the LINQ queries into Collaborative Application Markup Language (CAML) queries thus adding an extra step for retrieving the items. 

1 comment: