Do you use LINQ instead of CAML?

Updated by Brady Stroud [SSW] 1 year ago. See history

123
<introEmbed body={<> 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: 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: Figure: Good example – using LINQ

Acknowledgements

Andy Taslim
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star