linq sheet cheat code example

Example 1: linq cheat sheet

Dim query = from m in Models
		join p in PartNumbers on m.ModelId equals p.ModelId
		where m.Name.Contains("Domane")
		select new with {.ModelId = m.ModelId, .ModelName = m.Name, .PartNumber = p.InventoryPartNumber }

Dim lambda = Models.Join(PartNumbers, function(m) m.ModelId, function(p) p.ModelId, function(m,p) new with {.m = m, .p = p} ) _
					.Where( function(j) j.m.Name.Contains("Domane")) _
					.Select(function(j) new with {.ModelId = j.m.ModelId, .ModelName = j.m.Name, .PartNumber = j.p.InventoryPartNumber})

Example 2: linq cheat sheet

'Single() will throw an error if more than one result, or there is no result
	Dim lambda = Statuses.Single( function(s) s.Name = "Active")
	
	'SingleorDefault() will throw an error if more than one result, and null if there is no result
	Dim lambda = Statuses.SingleOrDefault( function(s) s.Name = "Active")

	'first() will throw an error if no results
	Dim lambda = PartNumbers.First( function(p) p.ModelId = 10001)
	
	'firstOrDefault will return null if no results
	Dim lambda = PartNumbers.FirstOrDefault( function(p) p.ModelId = 10001)
	
	Dim lambda = PartNumbers.FirstOrDefault()

Tags:

Misc Example