Instantiating a context in LINQ to Entities

Creating a new ObjectContext each time does involve ‘some’ overhead. Essentially the overhead involved is copying metadata from a global cache into metadata associated with the specific ObjectContext. This overhead is relatively minor, so often it is not worth worrying about, especially when you consider the extra safety inherent in the using pattern. For me … Read more

PowerShell equivalent of LINQ Any()?

To answer the immediate question with a PowerShell v3+ solution: (Get-ChildItem -Force -Directory -Recurse -Depth 2 -Include ‘_svn’, ‘.svn’).Parent.FullName -Directory limits the matches to directories, -Recurse -Depth 2 recurses up to three levels (children, grandchildren, and great-grandchildren), Include allows specifying multiple (filename-component) filters, and .Parent.FullName returns the full path of the parent dirs. of the … Read more

How to create a dynamic LINQ join extension method

I’ve fixed it myself now. It was a schoolboy error passing too many parameters to the CreateQuery(… ) call. Paste the following code into the Dynamic.cs file within the DynamicQueryable class for a dynamic Join extension method. You can find the source for the DynamicQuery sample project at http://code.msdn.microsoft.com/csharpsamples. Enjoy. public static IQueryable Join(this IQueryable … Read more

Sum of items in a collection

You can do LINQ to Objects and the use LINQ to calculate the totals: decimal sumLineTotal = (from od in orderdetailscollection select od.LineTotal).Sum(); You can also use lambda-expressions to do this, which is a bit “cleaner”. decimal sumLineTotal = orderdetailscollection.Sum(od => od.LineTotal); You can then hook this up to your Order-class like this if you … Read more

How to use LINQ in C++/CLI – in VS 2010/.Net 4.0

You can use the Linq methods that are defined in the System::Linq namespace, but you’ll have to jump through a couple extra hoops. First, C++/CLI doesn’t support extension methods. However, the extension methods are regular methods defined on various classes in System::Linq, so you can call them directly. List<int>^ list = gcnew List<int>(); int i … Read more