Tuesday, March 11, 2014

Mulitple "from" statement in LINQ and a Single Element Sequence example

I like to use multiple "from" statement in my LINQ query for two reasons:
1. It's easy to understand. It maps naturally to a nested loop like the following
   
    foreach(var outerElement in outSequence.Where(t => ...)) 
  {
     // outerElement can be used in the inner query
           foreach(var innerElement in outSequence.Where(t => ...))
    {
        ....
    }
  }

2. It is more flexible than "join" because there is not limit on how you use the outer element in inner query. As shown by the following example. 

Sometimes, you may want to select a single element sequence in LINQ. For example, once I needed to use several from statements and some from statement should return a single element sequence. In LINQ, it is pretty easy with a multiple from query:

from outerElement in outSequence
  .Where(t => ...)
from innnerElement in innerSequence
  .Where(t => ...)
  .DefaultIfEmpty()
  .Take(1);

The second "from" statement returns exactly one element sequence result. In the above example, the number of element of the above query result is the number of element returned from the first query.

Monday, March 10, 2014

Visual Studio NuGet package restore

Visual Studio has two approaches to use package restore: Automatic Package Restore and MSBuild-Integrated package restore.  The 'MSBuild-Integrated Package Restore' restores packages DURING the building process that might cause issues in some scenarios. The 'Automatic Package Restore' is the recommended approach by the NuGet team (http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore).

There are several steps to to make 'Automatic Package Restore' work:
1. In Visual Studio, Tools -> Extensions and Updates,  Upgrade NuGet if there is a newer version (Version 2.7 or later)

2. If you use TFS, in your solution's .nuget folder, remove the NuGet.exe and NuGet.targes files. Then edit NuGet.Config to not check in NuGet packages:

<configuration>
    <solution>
        <add key="disableSourceControlIntegration" value="true" />
    </solution>
</configuration>

If you checked in packages folder to TFS, delete the folder and check in the deletion of package folder deletion.

If you don't use TFS, delete the .nuget folder.

3. In each project file (.csproj or .vbproj) in your solution, remove the <Import Project> that references NuGet.targets file. The reference looks like this:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

Remove this line in every project file in your solution.

4. In Visual Studio menu, either through

Tools -> Options -> Package Manager -> General  or
Tools -> NuGet Package Manager -> Package Manager Settings

 please enable the following two options
1) 'Allow NuGet to download missing packages'
2) 'Automatically check for missing packages during build in Visual Studio'

5. Test your package restore configuration by the following steps
1) Save your solution and close Visual Studio
2) Delete your solution's packages folder
3) Start Visual Studio, open your solution and rebuild it.

Tuesday, March 4, 2014

In LINQ to Entities, the Include() method does not work with the Join() method

In using Entity Framework, we try to write queries that only incur one database roundtrip.  However, as described in this blog (http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx), when you introduce an intermediary join or something that change the “shape” of the query, the Include() method will throw a runtime exception stating “method cannot be translated into a store expression”. 

The DbExtensions.Include extension method requires IQueryable<T> where T is the entity type that the Include() method is applied. However, when a join operator is used to join multiple tables, the ‘shape’, i.e., the query result, is changed to a new entity type. The Include() method call fails with this new query result type.

There are two workarounds proposed by the above blog and a stackoverflow.com answer by the same author (http://stackoverflow.com/questions/794283/linq-to-entities-include-method-not-loading).
The first workaround has the following code sample:
var results = 
    ((from post in dbContext.Posts
     from author in dbContext.Authors
         where author == post.Author 
and author.email="abc@email.com"
     select post) as ObjectQuery<Post>).Include(“Comments”);
It requires the final select be an entity that keeps the same query shape to use the Include() method. It only works for a few simple queries.

The second workaround has the following code sample:
var results =
    
from post in dbContext.Posts
    from author in dbContext.Authors
         where author == post.Author 
and author.email="abc@email.com"
    select new { Post = post, Dummy = post.Comments };

The key is to put Dummy = post.Comments in the select clause. The "Dummy" property uses an Entity Framework featured called relationship fixup to populate the entity Properties. This is an elegant solution that works well. The blogger, Alex, explains the fixup in his EF Tips blog (http://blogs.msdn.com/b/alexj/archive/2009/02/25/tip-1-sorting-relationships-in-entity-framework.aspx ):  “In the Entity Framework Object Services automatically ties things together that are related in a process called Fix-up.  Fix-up generally occurs once the second of two related entities enter the context.”

The author (Alex D James)’s LINQ tips blog (http://blogs.msdn.com/b/alexj/archive/2009/03/26/index-of-tips.aspx) has many gems. 

.less (Dot Less), bootstrap 3 and font awesome in Asp.Net: configuration and workflow

Dot-less (www.dotlesscss.org), dot-less is a port of the popular LESS JavaScript library (lesscss.org) for the .NET platform.  It allows variables and mixins to define CSS styles in a more productive way.  The bootstrap (getbootstrap.com) is a flexible front end style framework based on LESS. Though there are many benefits of using bootstrap as the front end CSS framework to build a Web application, the CSS-based approach of using bootstrap requires one to write many bootstrap CSS classes in HTML. It is both ugly and not efficient for a big project and long term maintenance. Ideally we want to use bootstrap without mixing style formats and HTML code. This leads to use a LESS-only method to define all CSS styles in a Web application.
This article covers two subjects. First, we setup an ASP.NET project to use dotLess and configure it to work properly in both debug and release version. Then we define the workflow steps.

1.      Install and configure dotLess package

The following steps are based on Dino Esposito’s MSDN magazine article in http://msdn.microsoft.com/en-us/magazine/dn463774.aspx
1)      Use NuGet to install the latest DotLess package
PM> Install-Package dotLess
2)      Then add a new HttpHandler to Web.Config to support debug mode CSS file request from its LESS code.   There are two places: one for classic mode (IIS 6.0 or below) and one for integrated mdoe (IIS 7.0 and above)
<system.web><httpHandlers>
   <add path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler, dotless.Core" />
</httpHandlers></system.web>

<system.webServer><handlers>
  <
add name="dotless" path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />
</
handlers></system.webServer>
Optionally, we can specify a configuration section for the LESS/CSS request.
<configSections>
  <
section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />
</
configSections>
<dotless minifyCss="false" cache="true" web="false" />

3)      We need to configure bundling and minification
First, we need to use NuGet to install a LESS bundle transformer. A simple transformer described in the above MSDN article doesn’t understand file path. The System.Web.Optimization.Less package is what we want.
Then add all your less files to a bundle in your BundleConfig.cs
bundles.Add(new LessBundle("~/Content/Less").IncludeDirectory("~/Content/Less", "*.less"));
Finally, use the generated CSS file in your code Razor view file
@Styles.Render("~/Content/Less")

2.      The workflow

The bootstrap style workflow is based on the general bootstrap 3 less workflow tutorial described by Erik Flowers in his blog: http://www.helloerik.com/bootstrap-3-less-workflow-tutorial.
We put all less files in ~/Content/Less folder. There are two sub folders: one is bootstrap and another one is local. The bootstrap folder has all the LESS files downloaded from bootstrap packages. We also need a ~/Content/fonts folder to store all fonts used by bootstrap fonts. Copy all bootstrap font files to this folder.
Then define our variables, mixins and page-specific LESS files in the local folder.
Finally, use a Style.less file to import all used LESS files in the ~/Content/Less folder.

To use font-awesome, we need to create a font-awesome folder in ~/Content/Less folder and copy all font-awesome LESS files to the folder. Then copy all its fonts to the ~/Content/fonts folder. We need to change its font path to
@fa-font-path:        "../../fonts";
To make the body use auto width and keep the bootstrap .container style, add the following styles instead of .container;
.container-fixed();