In SharePoint development, it is always a good practice to use LINQ, instead of CAML.
Why CAML is bad?
- New language skills required for .NET developers
- No IntelliSense or strongly typed objects
Why LINQ is good?
- No new language skills required
- Easier to read and write
- SPMetal is awesome for generating entity classes
- In the backend, LINQ provider translates as much as it can to CAML first
SPQueryquery = newSPQuery();
query.Query= String.Format(“
<Where>
<And>
<Contains><FieldRefName=‘Tags’ /><ValueType=‘Text’>{0}</Value></Contains>
<IsNotNull><FieldRefName=‘URL’ /></IsNotNull>
</And>
</Where>
<OrderBy>
<FieldRefName=‘PostedOn’ Ascending=‘TRUE’ />
</OrderBy>”, _filter);
SPListItemCollectionlistItemsColl= resourceList.GetItems(query);
Figure: Bad example – using CAML
Var resourceListItems =
From SPListItem item in resourceList.Items
Where item.Tags.ToString().ToLower().Contains(_filter)
&& item.URL.ToString().Length> 0
OrderBy item.PostedOn Ascending
Figure: Good example – using LINQ