LINQ

LINQ

Eager Loading more than 1 table in LinqtoSql

When I've tried in Linq2Sql to load table with 2 child tables, I've noticed, that multiple SQLs are generated. I've found that it isa known issue, if you try to specify more than one to pre-load it just picks which one to pre-load and which others to leave deferred (simply ignoring those LoadWith hints)There are more explanations in http://codebetter.com/blogs... reason the relationship ......

Convert collections of enums to collection of strings and vice versa

Recently I needed to convert collections of strings, that represent enum names, to collection of enums, and opposite, to convert collections of enums to collection of strings. I didn’t find standard LINQ extensions.However, in our big collection of helper extensions I found what I needed - just with different names: /// <summary> /// Safe conversion, ignore any unexpected strings/// Consider to name as Convert extension /// </summary> /// <typeparam name="EnumType"></typ... ......

Specify Linq To SQL ConnectionString explicitly

When modifying Linq to Sql data model in Visual Studio 2010, it re-assigns ConnectionString that is available on developer’s machine. Because the name can be different on different machines, Designer often replace it with something like ConnectionString1, which causes errors during deployment.It requires developers to ensure that ConnectionString stays unchanged. More reliable way is to use context constructor with explicit ConnectionString name instead of parameterless default constructor GOOD: ......

Select First Row In Group using Custom Comparer

We have a requirement for a list of itineraries with multple itinerary items show only single itinerary in the list with details of one of the items selected based on some custom sorting logic. So I needed to group by itinerary ID and sort by custom Comparison and select the first in each group. Below is a LinqPad file that I've used for testing: void Main() { // http://smehrozalam.wordpres... // http://stackoverflow.com/qu... ......

Include MoreLinq Library for LINQ extensions

Last week I needed to find an object from collection with maximum value of the property and wrote the following double maxHours = (from loc in locations select loc.TotalHours).Max(); var location = (from loc in locations where (loc.TotalHours == maxHours) select loc).FirstOrDefault(); but wasn’t happy and checked the internet. The answer in http://stackoverflow.com/qu... referred to http://code.google.com/p/mo... ......

Use CompiledQuery.Compile to improve LINQ to SQL performance

After reading DLinq (Linq to SQL) Performance and in particular Part 4 I had a few questions. If CompiledQuery.Compile gives so much benefits, why not to do it for all Linq To Sql queries? Is any essential disadvantages of compiling all select queries? What are conditions, when compiling makes whose performance, for how much percentage? World be good to have default on application config level or on DBML level to specify are all select queries to be compiled? And the same questions about Entity Framework ......

Lookup Tables with fallback support

Our database has a few lookup tables that uses similar approach to implemented by .Net Framework for localized resources: At run time, ASP.NET uses the resource file that is the best match for the setting of the CurrentUICulture property. If there is no match for the current UI culture, ASP.NET uses resource fallback. It starts by searching for resources for a specific culture. If those are not available, it searches for the resources for a neutral culture. If these are not found, ASP.NET loads the ......

LINQ: check for null return from function

One of of my colleagues wrote a LINQ query similar to the following: var q=from i in keys select GetTuplesOrPossibleNull(i) into kvp orderby kvp.Item2 select kvp; I’ve noticed, that for some data it throws null exception on kvp.Item2 in orderby statement. It was found that calling function sometimes return null instead of expected key-value pair. I thought where I should insert check for null and my first idea was to add check in where statement before select function call, something like the following ......

Declare local variable within the loop instead of using loop variable in Linq methods

I've noticed in my LINQ code Resharper Warning "'Access to modified closure'". The search pointed that there is very confusing potential error. If for/foreach loop variable is used only in Linq methods (more general, only as a parameter for delegates) , only the last value of the variable will be used for all calls. It always required to create local variable inside loop and use the local variable instead of loop variable. More about the issue: Linq: Beware of the 'Access to modified closure' demon ......

«October»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678