CHunky Universe of Vigourous Astonishing SHarepoint :)

Tag Archives: LINQ

Set ObjectTrackingEnabled = false when reading

In LINQ 2 SP we work with a data context. By default a data context has the property ObjectTrackingEnabled = true. This property is crucial for all other operations in CRUD except Read. I performed a mini experiment. I created 20 000 items in my task list. Every seventh item contains “pärla”. Allright, here is what I found out:

2857 / 20 000 items
ObjectTrackingEnabled = true 07s.405ms
ObjectTrackingEnabled = false 01s.232ms

Sharepoint as a developer platformWith ObjectTrackingEnabled it takes 6 times more time to read the list items: whole 7,5 seconds. It would be a catastrophy if you would run this code to retrieve some items in your webpart which runs synchronously in the main thread.

The best SharePoint book from a developer perspective (SharePoint 2010 as a developer platform) explains ObjectTrackingEnabled:

Track Changes
The LINQ to SharePoint provider checks changes made in the database against its current state. This is
the default behavior. If you access the lists in a read-only manner, the tracking can be suppressed to
optimize performance:
ctx.ObjectTrackingEnabled = false;

Here is the code which I ran to get the results:

Console.WriteLine("Oppgaver, 20 000 items");
var sw = new Stopwatch();

Console.WriteLine("Retrieving items containing pärla");
Console.WriteLine("**** ObjectTrackingEnabled=true (default) ****");

using (var ctx = new OppgaverDataContext("http://takana/"))
	var tasks = ctx.Oppgaver.Where(t=> t.Title.Contains("pärla")).ToList();
	Console.WriteLine("\tcount: " + tasks.Count());


var elapsed = String.Format("{0:00}s:{1:000}ms", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
Console.WriteLine("\ttime:\t" + elapsed);

sw = new Stopwatch();
Console.WriteLine("**** ObjectTrackingEnabled=false ****");

using (var ctx = new OppgaverDataContext("http://takana/") { ObjectTrackingEnabled = false })
	var tasks = ctx.Oppgaver.Where(t => t.Title.Contains("pärla")).ToList();
	Console.WriteLine("\tcount: " + tasks.Count());

elapsed = String.Format("{0:00}s:{1:000}ms", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
Console.WriteLine("\ttime:\t" + elapsed);

Increase performance while retrieving data with LINQ to SP

If you are just intrested in getting data, not writing to the source like SubmitChange, you can disable ObjectTracking and increase the performance.

context.ObjectTrackingEnabled = false;

I found this tip on page 248 in the book “Sharepoint 2010 as a development platform

Komma åt andra tabeller via foreign keys i LINQ

I stället för att köra join-statements i LINQ som jag visat i ett av mina tidigare inlägg, kan man utnyttja inbyggda möjligheter att komma åt saker via foreign keys. Jfr de här två:

var q = from s in ctx.Staffs
        join si in context.StaffOnCourseInstances
        on s.uid equals si.uid
        where si.CourseInstanceId == 1435
        select new { s.adAccount };


var q = from c in context.StaffOnCourseInstances
        where c.CourseInstanceId == 1435
        select new { c.Staff.adAccount };

Visst är den andra varianten mycket smidigare och lättare att förstå och uppdatera vid behov. De tabeller som står i relation till den utgångstabellen kan lätt kommas åt via själva namnet på tabellen som har foreign key i den utgångstabellen. c.Staff.adAccount innebär att vi hämtar relaterade tuplar i tabellen Staff och väljer kolumnen adAccount.

join i LINQ

LINQ är ett kraftfullt verktyg. Det som kan vara lite krångligt är join. Men om man gör rätt blir det bra.

Låt oss titta på det sql-exemplet:

SELECT s.adAccount
FROM Staff s join StaffOnCourseInstance si
ON s.uid = si.uid
WHERE si.CourseInstanceId = 1435

För att köra motsvarande LINQ måste vi vara väldigt noga med on:

var testQuery = (from s in ctx.Staffs
            join si in ctx.StaffOnCourseInstances
            on s.uid equals si.uid
            where si.CourseInstanceId == 1435
            select new { s.adAccount });

Edit 20101031: Det finns ett smidigare sätt att komma åt information i relaterade tabeller. Se mitt inlägg från 20101031 om LINQ och foreign keys.

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

Paul J. Swider - RealActivity

RealActivity is a specialized healthcare services and solution advisory firm.

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders


Me and My doings!

Share SharePoint Points!!

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén


SharePoint for everyone


Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint