{%Pragmatic Coding%} Tips and tricks for software developers.

2Jan/140

Canonical Function “EntityFunctions.TruncateTime” does not exist in MYSQL

In our project we are using CodeFirst EF and MySQL database. And I had to make some queries that excludes time from comparison. But when I tried to use "EntityFunctions.TruncateTime" function as I could use it with regular LINQ and MS SQL I've got an error {"FUNCTION [database].TruncateTime does not exist"}.

While researching network I found that EF v.4 and upper should support this function, but unfortunately it does not work for me, so I have found best solution is to create custom function for MySQL:

And now you can simply use it in your code:

Hope this article was helpful.

Tagged as: C#, mysql, sql No Comments
2Jan/140

How to combine imported and local resources in WPF user control

Hi everyone! today I will show you how to use resources in WPF.

I have several modules in my application, and there is common module that contains general staff used in multiple modules. And I need shared resources. So here is a few steps we need to gain our goal.
Read the full article...

Tagged as: C#, wpf, xaml Continue reading
20Nov/130

Get the last day of the month in C#

Hello, friends!

Today I'll give you a snippet extension for DateTime to simply get the last date of needed month.

And usage example:

Thanks for attention.
I hope this will help you a lot.

Tagged as: C#, datetime No Comments
25Jul/130

C# – Get number of days in a year

Simple snippet function to get number of days for specified year.

Tagged as: C#, datetime No Comments
12Feb/130

How to: Using TPL Dataflow for multithreaded file compression

In this small tutorial I will show you how to use TPL Dataflow  library with a quite trivial task - multithreaded file compression.

PreInit

We need to implement efficient compression of files using GZipStream  class in the System.IO.Compression namespace. It is assumed that we will compress large files that can not fit entirely in memory. Read the full article...

15Jan/132

C# ASP.NET friendly “slug” url creating

Hello friends!

I had to develop asp.net mvc controller which could accept a parameter as a slug string for friendly URL's which look like the following:

First what should be done is adding new MapRote with modified url template and parameters:

Great, we now have formatted our route for blog record url. Now we need to wtite extension method which will generate clear formatted URL. Read the full article...

24Dec/122

How to: Using async and await in C # – Best Practices

Async and await keywords, introduced in C # 5.0, greatly simplify asynchronous programming. They also hide some difficulties, if you lose a focus. It can add some problems to your code. The following practices will serve you well if you create asynchronous code for. NET applications. Read the full article...

18Dec/120

What’s new in reflection for .NET 4.5

.NET 4.5 has some changes to the System.Reflection. The most significant is that the Type is now separated into two classes: Type and TypeInfo. TypeInfo object keeps a complete definition, and the Type now keeps only general information. If you use the reflection from your desktop or web applications for NET 4.5, then the old API is still available too, along with new reflections. Today I will focus on how to use some of the main features of the new API.  Read the full article...

15Dec/120

Dynamic Linq OrderBy Extension

Few days ago as usualy I working on my project and I made a function where I'm passing field name for sorting  table using Linq stuff. I was in need of an OrderBy extension method that could take a string field parameter in OrderBy and sort a IQueryable<> or IEnumerable<> collection. I was playing around implementation that could work, but I just wasn't satisfied with its internals (quite a bit of reflection to get the correct type to construct a LambdaExpression, etc) Read the full article...

Tagged as: .NET, C#, LINQ Continue reading
12Dec/120

How To: Get property value from string using reflection in C#

Few days ago I had to implement selection of a list of items with filtering and fields I had to filter by has been passed as a string value.
Will give you sample function where I have implemented filtering.

As you can see I'm using GetPropValue function that uses Reflection to get object property value.
Here is GetPropValue function implementation:

I hope this post was helpful for you!
Post a coment if you have any questions!