C# .NET: Create/Return Tuple List from Entity Framework EF

3/4/2020 4:24:17 PM

Use Entity Framework to return a list with only a few properties and create a Tuple List.



//assume db context is already setup

var user_list = new List<Tuple<int, string, string, string>>();

var query = from users in db_context.Users

			//maybe add a filter
			where users.FavoriteColor == "blue"

			select new
			{
				users.UserId,
				users.FirstName,
				users.LastName,
				users.Email
			};


user_list = query.Select(row => new Tuple<int, string, string, string>(
	row.UserId,
	row.FirstName,
	row.LastName,
	row.Email
	))
	//.Distinct()
	.ToList();