Thursday, March 22, 2012

Linq SelectMany and GroupJoin to do a join

Here's how to do a join between two arrays in a Linq Lambda expression:
var list1 = new int[] {1,2,3,4,5};
var list2 = new int[] {2,3};
var result = list1.SelectMany(l2=>list2, (l1,l2)=>new {l1,l2, sum=l1+l2})
.Where (x=>x.l1==x.l2)
.Select(x=> x); 

The result is this:




And now here is the GroupJoin version which actually does a nice left join:
 
var groupJoinResult = list1
 .GroupJoin(list2, 
  L1=>L1,
  L2=>L2,
  (l1, l2) => new {l1,l2}).Dump();

The result is this:

And finally to do a left join, use DefaultIfEmpty qualifier in inner query of select many, as shown in this stackoverflow q and a.

Monday, December 19, 2011

MongoDB Experiment

This example shows how to connect to a mogoDB database using the mongo-csharp-driver (git checkout). The code below demonstrates how to insert data into a collection, and retrieve a collection and deserialize the data.


using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver;

namespace Console1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //BsonClassMap.RegisterClassMap();
            RunCode();
        }

        private static void RunCode()
        {
            var settings = new MongoServerSettings();
            int portNumber=999;
            settings.Server = new MongoServerAddress("mongoServer.com", portNumber);
            settings.DefaultCredentials = new MongoCredentials("username", "password", false);
            MongoServer mongo = MongoServer.Create(settings);
            var dbSettings = new MongoDatabaseSettings(mongo, "databaseName");
            MongoDatabase mongoDb = mongo.GetDatabase(dbSettings);
            //this lists the names of the collections
            foreach (string collectoinName in mongoDb.GetCollectionNames())
            {
                Write(collectoinName);
            }

            
            AddCrimeTomongoDbAndReadBackTheList(mongoDb);

            Write("Press x to exit, or any other key to Loop!");
            ConsoleKeyInfo key = Console.ReadKey();
            if (key.KeyChar != 'x')
                RunCode();
        }

        private static void AddCrimeTomongoDbAndReadBackTheList(MongoDatabase db)
        {
            var crime = new Crime
                            {
                                Id = Guid.NewGuid(),
                                When = DateTime.Now,
                                Where = "Down town",
                                NatureOfCrime = "Purse snatching",
                                WitnessNames = new List {"Larry", "Curly", "Moe"}
                            };
            db.GetCollection("Crime").Save(crime);
            List crimes = db.GetCollection("Crime").FindAllAs().ToList();
            int i = 0;
            foreach (Crime c in crimes)
            {
                i++;
                Write(string.Format("item {2}: {1} (id {0})", c.Id, c.NatureOfCrime, i));
            }
        }

        private static void Write(string str)
        {
            Console.WriteLine(str);
        }
    }

    //this is a sample class which will be saved into mongoDB
    public class Crime
    {
        public Guid Id { get; set; }
        public string NatureOfCrime { get; set; }
        public string Where { get; set; }
        public DateTime When { get; set; }
        public List WitnessNames { get; set; }
    }
}

Wednesday, December 14, 2011

jsfiddle.net

If you are a javascript/html/css developer, you've got to see what these guys at jsfiddle.net have done.

Monday, November 21, 2011

Expression Blend 4 Step by Step By Elena Kosinska and Chris Leeds

A good tutorial for those who want to learn about Expression Blend and Silverlight

GetSkeleton

Skeleton is a small collection of CSS & JS files that can help you rapidly develop sites that look beautiful at any size, be it a 17" laptop screen or an iPhone. Skeleton is built on three core principles:



  1. Responsive Grid Down To Mobile
  2. Fast to Start
  3. Style Agnostic
Read more about it here: http://www.getskeleton.com/