LINQ to SQL Listing 2: Override Built-In Methods (VB.NET)

Typically, you need to implement several kinds of overrides for built-in LoadEntitySet() and LoadEntit­yRef() functions that you add to a partial DataContext file when you want to substitute stored procedures for LINQ to SQL's default dynamic prepared statements that populate associations. Neither VS 2008 beta 2 documentation nor the online help covers this syntax. Unfortunately, stored procedures are limited to a single entity per execution. Therefore, lazy loading or eager loading require the same number of server round-trips with stored procedures for EntitySets. Preloading EntityRefs can reduce round-trips to populate m:1 associations.

Partial Public Class NorthwindDataContext
	'Overrides to invoke stored procs instead of dynamic SQL

	'Following are for EntitySets
	Private Function LoadOrders(ByVal Cust As Customer) _
		As IEnumerable(Of Order)
		Return usp_GetOrdersByCustomerID(Cust.CustomerID)
	End Function

	Private Function LoadOrder_Details(ByVal Ord As Order) _
		As IEnumerable(Of Order_Detail)
		Return usp_GetOrder_DetailsByOrderID(Ord.OrderID)
	End Function

	'Following are for EntityRefs
	Private Function LoadCustomer(ByVal Ord As Order) _
		As Customer
		Return usp_GetCustomerByID(Ord.CustomerID). _
			FirstOrDefault()
	End Function

	Private Function LoadEmployee(ByVal Ord As Order) _
		As Employee
		Return usp_GetEmployeeByID(Ord.EmployeeID). _
			FirstOrDefault()
	End Function

	Private Function LoadShipper(ByVal Ord As Order) As Shipper
		Return usp_GetShipperByID(Ord.ShipVia). _
			FirstOrDefault()
	End Function
End Class
comments powered by Disqus

Featured

  • Hands On with GitHub Copilot App Technical Preview: Turning a Blazor Issue into a PR

    GitHub's brand-new Copilot desktop app, in technical preview, handled a small Blazor issue from planning through pull request creation, but the hands-on test also showed why developers still need to verify agent work in the running app before merging.

  • At Build 2026, Microsoft Sets Up Windows as an OS for AI Agents

    Microsoft's Build 2026 Windows developer announcements point to a broader platform strategy for agentic AI, spanning terminal workflows, local models, app-building skills, Cloud PCs and operating system-level containment.

  • Slammed by Copilot Usage-Based Billing on Day 1, Facing $180 Bill for June

    A journalist using GitHub Copilot Pro details how a broken editorial workflow on day one of usage-based billing led to runaway token consumption, a projected $180 monthly bill, and practical tactics for cutting AI credit burn.

  • AdaBoost.R2 Regression Using C#

    AdaBoost.R2 regression works by building an ensemble of decision trees, training them on reweighted data, and combining their predictions with a weighted median, while also showing how parameter choices affect accuracy and overfitting.

Subscribe on YouTube