Friday, 18 October 2013

Expression Lambdas vs Statement Lambdas

Lambda expression is an inline delegate introduced with C # 3.0 language. It’s a concise way to represent an anonymous method. Lambda expressions are particularly helpful for writing LINQ query expressions.

The => operator has the same precedence as assignment (=) and is right-associative.

Expression Lambda

A lambda expression with an expression on the right side is called an expression lambda.

Syntax
(input parameters) => expression

The parentheses are optional only if the lambda has one input parameter; otherwise they are required. Two or more input parameters are separated by commas enclosed in parentheses:

(x, y) => x == y

Sometimes it is difficult or impossible for the compiler to infer the input types. When this occurs, you can specify the types explicitly as shown in the following example:

(int x, string s) => s.Length > x

Specify zero input parameters with empty parentheses:

() => SomeMethod()

Statement Lambda

A statement lambda resembles an expression lambda except that the statement(s) is enclosed in braces:

Syntax
(input parameters) => {statement;}

Below example uses expression lambda and statement lambda to find even numbers from a List<int>

        List<int> numbers = new List<int>{1,2,3,4,5,6,7};
        var evens = numbers.FindAll(n => n % 2 == 0);
        var evens2 = numbers.FindAll((int n) => { return n % 2 == 0; });
Read More>>

dotnetperls/lambda

Exploring-Lambda-Expression-in-C#

Expression Lambda example - Find customers in UK

MSDN says "a lambda expression with an expression on the right side is called an expression lambda." Here is an example of Assume you have a List<Customer> where Customer has the following properties.
Customer {
int CustomerId,
string Name, 
string Country, 
string Email,
string Phone
}

If you want to create a List<Customer> where customer country = UK then you can use the below syntax.

List<Customer> ukCustomers =
Customers.FindAll(customer => customer.Country.ToUpper().Equals("UK"))

If you want to do the same thing using LINQ

List<Customer> ukCustomers = from Customer customer in Customers 
                where customer.Country.ToUpper().Equals("UK") 
                select customer 

LINQ query to create Dictionary

Assume you have a List<Customer> where Customer has the following properties.
Customer {
int CustomerId,
string Name, 
string Country, 
string Email,
string Phone
}

If you want to create a Dictionary of all customers who has an email address, i.e. Dictionary then you can use the below syntax.

Dictionary customerEmails = (from Customer customer in customers
                            where string.IsNullOrEmpty(customer.Email)
 select new { customer.CustomerId, customer.Email})
.ToDictionary(customer => customer.CustomerId, customer => customer.Email);

Below example shows how this can be done using Lambda Expression

Dictionary customerEmails =
Customers.FindAll(customer => !string.IsNullOrEmpty(customer.Email))
.ToDictionary(customer => customer.CustomerId, customer.Email);

Tuesday, 8 October 2013

C# Versions and features

These are the known versions of C#

  • Visual Studio 2013: No new C# and VB Language Features in VS 2013. But it has new versions of .NET Framework and ASP.NET i.e. .NET Framework 4.5.1 and ASP.NET 4.5.1. Click here to see what is new in VS 2013
  • C# 5.0 released with .NET Framework 4.5 and Visual Studio 2012 (August 2012). Major features: Asynchronous Programming with async and await, caller info attributes.
  • C# 4.0 released with .NET Framework 4 and Visual Studio 2010 (April 2010). Major new features: late binding (dynamic), delegate and interface generic variance, more COM support, named arguments and optional parameters
  • C# 3.0 released with .NET Framework 3.5 and Visual Studio 2008 (November 2007). Major new features: LINQ (Language Integrated Query), lambda expressions, extension methods, expression trees, anonymous types, implicit typing (var), One-step object creation and initialization, One-step collection creation and initialization, Type Inference, Automatic properties, Func and Action generic delegates
  • C# 2.0 released with .NET Framework 2.0 and Visual Studio 2005 (November 2005). Major new features: generics, anonymous methods, nullable types, iterator blocks
  • C# 1.2 released with .NET Framework 1.1 and Visual Studio 2003 (April 2003).
  • C# 1.0 released with .NET Framework 1.0 and Visual Studio 2002 (January 2002)

Click here to read about ASP.NET MVC Versions and Features

New features in C# 6

New features in C# 5

Microsoft Visual Studio on Wiki

.NET Framework versions and Dependencies

http://csharpindepth.com/Articles/Chapter1/Versions.aspx

Friday, 4 October 2013

Build XML Site Map online

www.xml-sitemaps.com helps you to create an XML sitemap that can be submitted to Google, Bing, Yahoo and other search engines to help them crawl your website better.

You can also generate an HTML site map to allow human visitors to easily navigate on your site.

Tuesday, 1 October 2013

How to print part of rendered html page?

This can be done using CSS and JavaScript as well. See examples below.

Print part of the page using CSS

To do it using CSS you need to apply @media specific styles to page content which you want to print and non-printable stuff.

<html>
<head>
    <style type="text/css">

    #printable { display: none; }

    @media print
    {
     #non-printable { display: none; }
     #printable { display: block; }
    }
    </style>
</head>
<body>
    <div id="non-printable">
     Your normal page contents
    </div>

    <div id="printable">
     Printer version
    </div>
</body>
</html>

Print part of the page using JavaScript

To achieve the same functionality using JavaScript you need to use an iframe and set it's innerHTML based on what you want to print.
<html>
<head>
<title>Print Test Page</title>
<script>

function printDiv(divId) {
    window.frames["print_frame"].document.body.innerHTML=
       printDivCSS + document.getElementById(divId).innerHTML
    window.frames["print_frame"].window.focus()
    window.frames["print_frame"].window.print()
}
</script>
</head>
<body>
<b>Div 1:</b> <a href=javascript:printDiv('div1')>Print</a><br>
<div id=div1>This is the div1's print output</div>
<br><br>
<b>Div 2:</b> <a href=javascript:printDiv('div2')>Print</a><br>
<div id=div2>This is the div2's print output</div>
<br><br>
<iframe name=print_frame width=0 height=0 
frameborder=0 src=about:blank></iframe>
</body>
</html>