Pages

Monday, July 30, 2012

C# LINQ - Take, Skip, TakeWhile, SkipWhile clauses

This post is part of a series called LINQ Examples that brings practical examples of using LINQ in C#.

Today we'll see practical examples of using Take, TakeWhile, Skip and SkipWhile clauses in LINQ. This kind of operators are called LINQ Partitioning Operators.
int[] numbers = { 1, 3, 9, 8, 6, 7, 2, 0, 5, 10 };

var firstFive = numbers.Take(5);
Console.WriteLine("First five numbers:");
foreach (var x in firstFive)
    Console.Write(x + ", ");    /*    Output:    1, 3, 9, 8, 6    */

var skipFive = numbers.Skip(5);
Console.WriteLine("All but first five numbers:"); 
foreach (var x in skipFive)
    Console.Write(x + ", ");    /*    Output:    7, 2, 0, 5, 10    */

var firstLessThanFive = numbers.TakeWhile(n => n < 5);
Console.WriteLine("First numbers less than five:"); 
foreach (var x in firstLessThanFive)
    Console.Write(x + ", ");    /*    Output:    1, 3    */

var fromFirstEven = numbers.SkipWhile(n => n % 2 != 0);
Console.WriteLine("All elements starting from first even element:");
foreach (var x in fromFirstEven)
    Console.Write(x + ", ");    /*    Output:    8, 6, 7, 2, 0, 5, 10    */

Saturday, July 28, 2012

WPF DataGrid ColumnHeaderStyle (LinearGradientBrush)


This post is part of series called WPF Styles where you can find many different styles for your WPF application.

Due to several requests to share XAML code of the DataGrid style that been used in my examples, today I'm sharing with you a ColumnHeaderStyle. Just to remind, this is how the DataGrid looks like:

WPF DataGrid ColumnHeaderStyle LinearGradientBrush

WPF DataGrid RowStyle (AlternationIndex)


This post is part of series called WPF Styles where you can find many different styles for your WPF application.

Today I will share with you a WPF DataGrid RowStyle that I used recently for my DataGrid. At some point of development I noticed that data inside my DataGrid was really hard to read because of each row had the same color, so I decided to apply a style that will alternate the color for the rows, just like we usually see in excel reports. After applying this style your DataGrid will look like this:

WPF DatagridRow style with AlternationIndex set to 1

Tuesday, July 24, 2012

WPF DataGrid CellStyle (CenterCellStyle)


This post is part of series called WPF Styles where you can find many different styles for your WPF application.

When your DataGrid contains many different controls, the height of a cell in DataGrid increases accordingly. This make your text to look ugly inside the cell.

DataGrid without CellStyle

Sunday, July 22, 2012

C# LINQ - SELECT clause

This post is part of a series called LINQ Examples that brings practical examples of using LINQ in C#.

This time we'll concentrate our attention on SELECT clause. This kind of operators are called LINQ Projection Operators. First example shows use of SELECT with Anonymous Types. We produce a sequence of employees containing employee name and create a new type on the fly called ProjName which indicates the first project that specific employee has participated.
List<Employee> employees = GetEmployeeList();

var emps =
    from e in employees
    select new { e.EmployeeName, ProjName = e.Projects[0].ProjectCode };

foreach (var e in emps)
{
    Console.WriteLine("{0} worked on project {1}.", e.EmployeeName, e.ProjName);
}

/* Output:   
John worked on project Orlando.
Tomas worked on project Orlando.
Marina worked on project Orlando.
Susan worked on project Rocket.
Lucas worked on project Orlando.
*/

Friday, July 20, 2012

C# LINQ - WHERE clause

This post is part of a series called LINQ Examples that brings practical examples of using LINQ in C#.

Today I'll show how to use WHERE clause in LINQ. This kind of operators are called LINQ Restriction Operators. We start from the simplest use of WHERE clause in LINQ - select all numbers from an array less than 10:
int[] numbers = { 15, 42, 14, 3, 9, 83, 64, 76, 23, 0 }; 
      
var lessThanTen = 
    from n in numbers 
    where n < 10 
    select n; 

foreach (var x in lessThanTen) 
{ 
    Console.Write(x + ", ");  // 3, 9, 0
}

Thursday, July 19, 2012

C# Load XML using XLINQ (LINQ to XML)


Today I'll show how to load an XML file into objects using XLINQ (Language Integrated Query for XML).
Imaging that you have an XML file which contains an Employee list with corresponding information about each employee. Now you want to load this list into memory and work with that data. For these purposes we have XLINQ technology which allows you to load the data from XML directly into business objects. 
Let's see how to use XLINQ in C#. First, we need to create our business objects:

Monday, July 16, 2012

Windows Service in C# with Setup


Today I'll show how to build Windows Service in C# using Visual Studio 2010.
First, open Visual Studio and create Windows Service project.

Windows Service Project in Visual Studio 2010

Thursday, July 12, 2012

C# XML Transformation using XSLT


If you have one type of XML and need to transform it to another type, then you can use XSLT for that purposes. For instance in the health care field, there are a lot of XML standards, such as CCD, CCR etc. Sometimes one type of XML need to be transformed to another one. The best way to do it is to use XSLT. You may ask where do you get appropriate XSLT for specific XML transformation? Well, for example I use Altova MapForce. MapForce allows you to do XML transformation manually by simply dragging appropriate attributes. Once you done with that, MapForce will generate an XSLT which you can use then in your code.
Altova MapForce XML Transformation and XSLT Generation
Ok, we got an XSLT file, here is how we use it in C# code in order to transform XML file:

C# Polymorphism Example


How often we use Polymorphism in our programs? Polymorphism is the core of any modern object oriented programming language and we use it daily almost without thinking of it. But do we remember the basics of Polymorphism? Recently I realized that I forgot simple stuff when I was on a job interview. Therefore today I want to remind about Polymorphism basics in C#. Hopefully it will help you to pass your next job interview.
Ok, let's write three simple classes and play with Polymorphism a little bit:

Tuesday, July 3, 2012

WPF DataGrid RowDetailsTemplate

WPF DataGrid provides a Row Details feature. Row details is simply a panel that shown for a selected row. Row Details may contain any thing you want to put there - label, textbox, button, picture...
WPF DataGrid RowDetailsTemplate example