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:
1 2 |
Create FUNCTION TruncateTime(dateValue DateTime) RETURNS date return Date(dateValue); |
And now you can simply use it in your code:
1 2 |
_repository.SingleOrDefault( p => EntityFunctions.TruncateTime(p.Date) == DateTime.Now.Add(-1).Date); |
Hope this article was helpful.
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...
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.
1 2 3 4 5 6 7 |
public static class DateExtensions { public static DateTime GetEndOfMonthDate(this DateTime date) { return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); } } |
And usage example:
1 2 3 4 |
var value = DateTime.Now; expiryDate = value.GetEndOfMonthDate(); // f.e. value = "20.11.2013" // result: expiryDate will = "30.11.2013" |
Thanks for attention.
I hope this will help you a lot.
C# – Get number of days in a year
Simple snippet function to get number of days for specified year.
1 2 3 4 5 6 7 8 9 10 |
/// Count number of days in a year /// /// Integer representation of a year. i.e. DateTime.Now.Year /// public static int GetDaysInYear( int year ) { var thisYear = new DateTime( year, 1, 1 ); var nextYear = new DateTime( year + 1, 1, 1 ); return (nextYear - thisYear).Days; } |
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...
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:
1 |
http://mysite.com/blog/my-simple-blog-post |
First what should be done is adding new MapRote with modified url template and parameters:
1 2 3 4 5 |
routes.MapRoute("BlogRote", // Route name "{controller}/{action}/{blogURLName}", new { controller = "Blog", action = "Index", blogURLName = ""} // Parameter defaults ) |
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...
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...
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...
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...
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private IEnumerable GetListItems(int startIndex, int pageSize, IDictionary<string, string=""> filter) { using (var db = Repository.Instance.GetNewDatabase()) { var refs = db.Object.AsEnumerable(); if (filter != null && filter.Count > 0) { //Apply filter refs = filter.Aggregate(refs, (current, fObj) => current.Where(p => ((String)GetPropValue(p, fObj.Key)).ToLower().Contains(fObj.Value.ToLower()))); } return refs.Skip(startIndex).Take(pageSize).ToList(); } } |
As you can see I'm using GetPropValue
function that uses Reflection to get object property value.
Here is GetPropValue
function implementation:
1 2 3 4 |
public static object GetPropValue( object src, string propName ) { return src.GetType( ).GetProperty( propName ).GetValue( src, null ); } |
I hope this post was helpful for you!
Post a coment if you have any questions!