LINQ: Finding out duplicates in the list with multiple fields

I needed very quick simple “one line” solution to filter out duplicates in my list. So I have a List<T> where T is complex object with multiple properties. I wanted to filter out non unique items based on two fields. In database analogy my list is table with two columns based composite primary key and I need to select non-distinct values.

Here is sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqDuplicates
{
    public class Item
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List items = new List();
            items.Add(new Item { ID = 0, Name = "John" });
            items.Add(new Item { ID = 1, Name = "Thorsten" });
            items.Add(new Item { ID = 1, Name = "Thorsten" });
            items.Add(new Item { ID = 2, Name = "Rapunzel" });
            items.Add(new Item { ID = 3, Name = "Rapunzel" });
            items.Add(new Item { ID = 3, Name = "Grinch" });

            var duplicates = items.GroupBy(x => new {x.ID, x.Name})
                            .Where(g => g.Count() > 1)
                            .Select(g => g.Key);

            foreach (var val in duplicates)
            {
                Console.WriteLine(val.ID + " " + val.Name);
                Console.ReadLine();
            }

        }
    }
}

So this code will select just one duplicate I have in list and that is Item with ID=1 and Name=”Thorsten”.

Another way around to get all distinct values would be:

var distinct = items.Select(item => new { item.ID, item.Name }).Distinct();

This results in list with all the distinct values based on two fields (composite key) criteria.

linq-distinct

Node.js Blueprints book review

Couple of months ago I did a book review for PacktPub for a Node.js beginner level book. Today I got a great news the book is finally published and available. It is a very simple 270 pages read and follow trough book. It touches up on many other popular frameworks and tools like AngularJS, Socket.io, BackboneJS, EmberJS, GruntJS. I would definitely recommend it for someone looking for quick start with Node.js.

7338OS_Node.js Blueprints

OS X 10.10 Yosemite on Virtual Machine

Apple just announced next version of OS X and I wanted to give it a try. Beta will be available for first million subscribers or you can get it from third party sites. I couldn’t wait for notification mail from Apple so I got my copy from here. Since this is a beta I didn’t want to mess up my current system so virtual machine was better choice.   I’ve used Oracle Virtual Box for OS X but it works with VMware Fusion or Parallels as well. Basically it’s an upgrade of you current OS X system so you will need running OS X virtual machine. There is a ton of tutorials how to install OS X on virtual machine. I’ve had OS X 10.8 and upgrade went smoothly. Simply extract and copy installer to your virtual machine and run it. It will restart two times and that’s about it.

I’ve used my 2011 iMac as a host with Intel Core i5 processor so experience is not that impressive. It is obvious that system is still in Beta. On the other hand UI looks pretty nice in my opinion but with all that translucency it’s starting to look very much like Windows.

Here are some screenshots of running system.

1. Login Screen

1. login screen     

2. Apple ID

2. appleid

 

3. Desktop & Screen Saver

3. Desktop background

 

 

4. Show all

 

4. System Info

5. System Info

5. 6. Safari Safari

 

 

 

6. Spotlight 8. Spotlight

 

7. Lunchpad

9. Lunchpad

Qt: Detect in the .pro file if compiling on 32 bit or 64 bit platform

QtLogo-CodeLess

Couple of months earlier I wrote how to compile Qt projects (.pro files) from command line both on OS X  and Windows. Basically it comes down to calling qmake executable and passing path to your .pro file and path to make spec file for specific compiler. It goes something like this:

C:\QT5_build\bin\qmake.exe [Path to your .pro file] 
-r spec C:\QT5_build\mkspecs\win32-msvc2010

I am using shared .pri configuration files which I later include in my .pro files to have consistent settings across different project. In my base .pri settings files I define where are shared libraries, include files, platform specific compiler flags.

One of the things I want to control is where should output go, namely DESTDIR. I wanted to for my 32 bit builds to land in Output folder and 64 bit in Outputx64, both on Windows and OS X. Here is my shared configuration .pri file:

BUILD_TARGET = SimpleProjectName

win32{
	PROJECT_FOLDER = ..\\..
}
else{
	PROJECT_FOLDER = ../..
}


macx-clang{
    OUTPUT_FOLDER = $$PROJECT_FOLDER/Outputx64
}

macx-clang-32{
    OUTPUT_FOLDER = $$PROJECT_FOLDER/Output
}

win32{
	SOURCE_FOLDER = $$PROJECT_FOLDER\\Source
	OUTPUT_FOLDER = $$PROJECT_FOLDER\\Output$$(Platform)
	TEMP_FOLDER =   $$OUTPUT_FOLDER\\Temp
	MODULE_TEMP_FOLDER = $$TEMP_FOLDER\\$$BUILD_TARGET
	GENERATED_FOLDER = $$OUTPUT_FOLDER\\Generated
	MODULE_GENERATED_FOLDER = $$MODULE_TEMP_FOLDER\\Generated
}
else{
	SOURCE_FOLDER = $$PROJECT_FOLDER/Source
	TEMP_FOLDER = $$OUTPUT_FOLDER/Temp
	MODULE_TEMP_FOLDER = $$TEMP_FOLDER/$$BUILD_TARGET
	GENERATED_FOLDER = $$OUTPUT_FOLDER/Generated
	MODULE_GENERATED_FOLDER = $$MODULE_TEMP_FOLDER/Generated
}

CONFIG(debug, debug|release): CONFIGURATION = Debug
CONFIG(release, debug|release): CONFIGURATION = Release

win32{
	CONFIGURATION_OUTPUT_FOLDER = $$OUTPUT_FOLDER\\$$CONFIGURATION
	CONFIGURATION_MODULE_TEMP_FOLDER = $$MODULE_TEMP_FOLDER\\$$CONFIGURATION
}
else{
	CONFIGURATION_OUTPUT_FOLDER = $$OUTPUT_FOLDER/$$CONFIGURATION
	CONFIGURATION_MODULE_TEMP_FOLDER = $$MODULE_TEMP_FOLDER/$$CONFIGURATION
}

# set intermediate directories
win32{
	UI_DIR = $$GENERATED_FOLDER\\Qt\\$$BUILD_TARGET
	RCC_DIR = $$CONFIGURATION_MODULE_TEMP_FOLDER
	MOC_DIR = $$MODULE_GENERATED_FOLDER
	OBJECTS_DIR= $$CONFIGURATION_MODULE_TEMP_FOLDER
}
else{
	UI_DIR = $$GENERATED_FOLDER/Qt/$$BUILD_TARGET
	RCC_DIR = $$CONFIGURATION_MODULE_TEMP_FOLDER
	MOC_DIR = $$MODULE_GENERATED_FOLDER
	OBJECTS_DIR= $$CONFIGURATION_MODULE_TEMP_FOLDER
}

DESTDIR = $$CONFIGURATION_OUTPUT_FOLDER

With this configuration my project files are normally under "Source" folder and then I will get to right next to it Output or Outputx64 depending on -arch I am using. Inside Output folder there will be Debug or Release folder, based on configuration I am compiling. Next to Debug and Release there will be folders for Temp and Generated files. Something like this:

build output

Key lines for mac OS X are number 11 and number 15. macx-clang is 64bit build configuration and macx-clang-32 is 32 bit build configuration. On line 21 I set Windows build configuration based on $$(Platform) variable. This will in case of 64 bit build add “x64” to “Output”. This flag works only for windows as far as I know. 

Note: In order to be able to build both 32 and 64 bit executables with Qt on OS X platform Qt must be also compiled for desired architecture.  To be able to build Qt on OS X for 32bit architecture Qt build must be configured with -platform macx-clang-32 flag before calling make command.   For example:

./configure -opensource -confirm-license -platform macx-clang-32 -debug-and-release -no-compile-examples ….

About me

Bizic Bojan is software developer, father, husband, blogger, hobby photographer and still trying to become much more. By day working at GMG Color GmbH in Research and Development. By night working on my own projects.

Disclaimer:

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.