Skip to content

Instantly share code, notes, and snippets.

@anlai
Last active December 17, 2015 20:59
Show Gist options
  • Save anlai/5672028 to your computer and use it in GitHub Desktop.
Save anlai/5672028 to your computer and use it in GitHub Desktop.
Better way to write linq queries
// inline method
var students = (
from student in RepositoryFactory.StudentRepository.Queryable
select new { FirstName = student.FirstName, LastName = student.LastName, FullName = string.Format("{0} {1}", student.FirstName, student.LastName) }
).ToList();
// using the let key word, looks cleaner
var students = (
from student in RepositoryFactory.StudentRepository.Queryable
let fullname = string.Format("{0} {1}", student.FirstName, student.LastName)
select new { FirstName = student.FirstName, LastName = student.LastName, FullName = fullname }
).ToList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment