Filtering LINQ To SQL using Object comparison

One of the recent trick I discovered with linq to SQL queries is, when you want to filter records, you do not necessarily have to filter by primary key like CustomerID. Instead you can compare object references and linq to SQL in the background would convert the linq query as comparison based on primary key of that table. For instance if I want to find all the orders for a particular customer, I would write a query like this

image

The above query is an explicit intent but sometimes too long to write. If all you are doing is comparing based on primary key column, you can simply compare object references and linq to SQL would filter the record in the database using primary key of that table. Here is same query written but instead filter is applied based on object comparison.

image

1 Comment

Comments have been disabled for this content.