DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

2 1

C# : Advanced LINQ - Use Subquery and Left Outer Join

LINQ is great tool to write query for any data source, but I sometime have no idea how to write correct code to execute what I want to run.

I am writing this blog for myself as I am sure to forget this sooner or later :)

Left outer join

Perform left outer joins explains details of how to write left outer join with LINQ.

The point is to use into and DefaultIfEmpty() method on it.

var query = from person in people
   join pet in pets on person equals pet.Owner into gj
   from subpet in gj.DefaultIfEmpty()
   select new 
   {
       person.FirstName, 
       PetName = subpet?.Name ?? String.Empty 
   };
Enter fullscreen mode Exit fullscreen mode

Subquery

Subquery is more straight forward. We can use subquery both in new section and in section. If query gets longer, we can also define subquery as separate query and use it in main query.

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay