LINQ

LINQ

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...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910