Click here to Skip to main content
15,897,032 members
Articles / Programming Languages / C# 4.0

Entity Framework Patterns: Select Multiple Entities

Rate me:
Please Sign up or sign in to vote.
4.69/5 (26 votes)
16 Dec 2010Ms-PL4 min read 175.4K   1.1K   47  
Introduce different patterns in Entity Framework around selecting a list of entities.
using System;
using System.Collections.Generic;
using System.Linq;

namespace TestPatterns
{
	/// <summary>Here we select each entity one at the time.</summary>
	public static class SelectingEach
	{
		public static Order[] DoSelect(int[] ids)
		{
			using (var context = new EFDemoEntities())
			{
				var list = new List<Order>(3);

				foreach (var id in ids)
				{
					var query = from o in context.Order.Include("OrderDetail")
								where o.OrderID == id
								select o;
					var objectQuery = (System.Data.Objects.ObjectQuery)query;
					var sql = objectQuery.ToTraceString();

					list.AddRange(query);
				}

				return list.ToArray();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect CGI
Canada Canada
Vincent-Philippe is a Senior Solution Architect working in Montreal (Quebec, Canada).

His main interests are Windows Azure, .NET Enterprise suite (e.g. SharePoint 2013, Biztalk Server 2010) & the new .NET 4.5 platforms.

Comments and Discussions