Maxime FRAPPAT

Hum …no thanks ! – Lordinaire

Author: Maxime FRAPPAT (Page 1 of 18)

Intersection between two lists of dates

I faced a problem when I tried to get the intersection between two lists of DateTime. I wanted to have the intersection only by comparing dates and not dates and time. If I use the LINQ method Intersect on my lists, it will not fit what I want because if the date match but not the time (like different seconds) it will not be part of the intersection.

Of course, I can make my own Intersect method that will test only the date but it’s a little bit overkill. The easy way is to use an IEqualityComparer and pass it to the Intersect method. Here is the implementation I used :

public class DateEqualityComparer : IEqualityComparer<DateTime>
{
    public bool Equals(DateTime date1, DateTime date2)
    {
        return GetHashCode(date1) == GetHashCode(date2);
    }

    public int GetHashCode(DateTime date)
    {
        var hashCode = date.Year ^ date.Month ^ date.Day;
        return hashCode.GetHashCode();
    }
}

A little sample of the case :

public void IntersectDates()
{
    var dates1 = new List<DateTime>
    {
        DateTime.Now,
        DateTime.Now.AddDays(1).AddSeconds(5),
        DateTime.Now.AddDays(3).AddSeconds(10)
    };
    // 05/10/2017 21:40:24
    // 06/10/2017 21:40:29
    // 08/10/2017 21:40:34

    var dates2 = new List<DateTime>
    {
        DateTime.Now,
        DateTime.Now.AddDays(2).AddSeconds(15),
        DateTime.Now.AddDays(3).AddSeconds(20)
    };
    // 05/10/2017 21:40:24
    // 07/10/2017 21:40:39
    // 08/10/2017 21:40:44

    var dateEqualityComparer = new DateEqualityComparer();
    var intersectList = dates1.Intersect(dates2, dateEqualityComparer).ToList();
    Log(intersectList);
}

private static void Log(IEnumerable<DateTime> list)
{
    foreach (var someClass in list)
    {
        Console.WriteLine(someClass.ToString());
    }
    // 05/10/2017
    // 08/10/2017
}

Work with Docker in Visual Studio 2017

Docker is a well-know tools in Microsoft ecosystem since few years. You can of course doing great things if you deal with Azure but it isn’t limited to “ops”. Developers can now easily play with Docker inside Visual Studio with some additional tools. Stuff like starting a container becomes so easy that everybody must use it! (I like Docker, a lot!)

Setup & Tools

First, you need to download and install Docker for Windows and select the .NET Core option in the installer of Visual Studio 2017.

Bonus: You can additionally download Kitematic (Visual Docker Container Management) if the CLI is not your friend.

Project sample

Let’s create a new ASP.NET Core 2.0 project of type WebAPI. The main benefit to target a .NET Core project is that it can run on Windows AND Linux platform.

Docker for Windows is able to run both of platforms type for a container but not a mixed use of it. So, you need that all your containers target the same platform, you can’t set a specific platform for a container. That’s the only limitation that I face off (and it can be a huge one…).

You can check the Enable Docker Support now or add it after when the solution will be created. If you doesn’t set the option, le solution explorer looks like that :

To activate the Docker support, right clic on the project and select Add > Enable Docker Support. Visual Studio will now automatically create a new project named docker-compose and a Dockerfile in your project. Well, thank you VS.

Dockerfile

FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

docker-compose.yml

version: '3'

services:
  webapplication1:
    image: webapplication1
  build:
    context: ./WebApplication1
    dockerfile: Dockerfile

Start the project

Before starting the project, we need to check if the shared drives settings are set properly because Docker required full access to drives where the source code.

To run your project with Docker, you need to set the docker-compose project as startup project. Hit F5 and open the console window : you will see all the steps like downloading the image, creating the container, …

Bonus: You can add breakpoints like you always do and it will work!

Bonus 2: If you launch Kitematic you will see your container with all his settings

 

 

 

Page 1 of 18

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: