a developer's notes – a semi-technical web development BLOG

March 31, 2013

Action and Func delegates in .NET extension methods

Filed under: C# — Duy Nguyen @ 7:11 pm
Tags: , , , , ,

The Action delegate never returns a value.

// no params
Action printEmptyLine = () => Console.WriteLine();

// one param
Action printNumber = x => Console.WriteLine(x);

// two params
Action<int, int> printTwoNumbers = (x, y) =>
                 {
                     Console.WriteLine(x),
                     Console.WriteLine(y)
                 };

A Func delegate returns some sort of value.

// at minimum, you have to specify the return type
Func getTime = () => DateTime.Now; // returns a type DateTime

// two params. The last param is the return type.
Func<int, int> square = x => x * x; // takes an int, returns an int

// three params
Func<int, int, int> multiple = (x, y) => x * y; // takes an int, returns an int

Using the delegates, you can just invoke them like this:

// using Action delegates
printEmptyLine();
printNumber(3);
printTwoNumbers(5, 10);

//using Func delegates
DateTime now = getTime();

Now when you use extension methods, will understand what it needs.

For example:
whereFunc
So the above, you know the where method takes a Employee type and returns a bool.

A Linq Expression

//This is the same Func delegate but there is an Expression Type around it.
Expression<Func<int, int, int>> multipleExpression = (x, y) => x * y; // takes an int, returns an int

You can’t just invoke expressions. You’d have to turn them back into delegates to use. Like this:

Func<int, int, int>> multipleExpression = multipleExpression.Compile();

Expressions are mainly used for Linq to SQL. Func and Action delegates are used in Linq to Objects.

When using any extension method, if you see a parameter for a Func, Action, or Expression, they all accept lambda expressions.

Advertisements

March 28, 2013

Padding against an empty div

Filed under: CSS / Styling — Duy Nguyen @ 2:56 pm
Tags: , , , ,

Some times you need to “push” against an empty div, you can use a div with an empty value like this.

<div style="padding-bottom:50px"></div>
<div>&nbsp;</div>

March 27, 2013

$document.ready() Shortcut in jQuery

Filed under: Javascript / JQuery,Uncategorized — Duy Nguyen @ 11:52 am
Tags: , , , ,

You can just use this:

$(function() {
     // do something on document ready
});

It is the same as this:

$(document).ready(function(){
     // do something on document ready
});

But if you’ve placed all your scripts right before the body closing tag, this is the same as $(document).ready.

How to debug into javascript/jquery when working with Visual Studio

Filed under: ASP.NET,Javascript / JQuery — Duy Nguyen @ 12:12 am
Tags: , , ,

Add debugger in your javascript code.

Within your Javascript code, add the line

debugger;

1. Using Visual Studio:
You can execute (F5) your application and debug with Visual Studio and the application will stop on your line of code. Note** You have to use Internet Explorer if you want to use Visual Studio.

js_vs

2. Use Chrome:
Just add the same line of code but have the Developer Tools open when you navigate to your page that executes your javascript code.

js_chrome

3. Use Firefox:
Just add the same line of code but have Firebug open when you navigate to your page that executes your javascript code.

js_firefox

March 12, 2013

The type initializer for ‘System.Data.Entity.Internal.AppConfig’ threw an exception.

I had a class library project that contains the Entity Framework .edmx file. In the same solution, I had a console application that consumed the class library project. But when I tried to run the console application to run a simple query on the DbContext object, got this error:

The type initializer for ‘System.Data.Entity.Internal.AppConfig’ threw an exception.

In the app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="AdventureWorksLTEntities" connectionString="metadata=res://*/AWModel.csdl|res://*/AWModel.ssdl|res://*/AWModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DNGUYEN-DESKTOP;initial catalog=AdventureWorksLT;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

Make sure the <startup> element is AFTER the <connectionStrings> element.

Create a free website or blog at WordPress.com.