Skip to content

Instantly share code, notes, and snippets.

@linuxbender
Created May 12, 2011 12:39
Show Gist options
  • Save linuxbender/968418 to your computer and use it in GitHub Desktop.
Save linuxbender/968418 to your computer and use it in GitHub Desktop.
Linq and C# - Join example
long tblUnitID = 808;
var _rec = (from u in _db.tblUnits
join ut in _db.tblUnitTypes on u.tblUnitTypeID equals ut.tblUnitTypeID into gj
from subset in gj.DefaultIfEmpty()
where u.tblUnitID == tblUnitID
select new { u.tblUnitID, u.tblUnitName, u.tblUnitTypeID, tblUnitTypeName = (subset == null ? String.Empty : subset.tblUnitTypeName) }).FirstOrDefault();
Console.WriteLine("{0} {1} {2} {3}",_rec.tblUnitID, _rec.tblUnitName, _rec.tblUnitTypeID, _rec.tblUnitTypeName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment