Fasil Hayat's blog

...C#, .Net, Sharepoint, BizTalk, JQuery...

Say you have the need to flatten a collection of albums and retrieve all the photos within the albums and put them into a single album, you can use the SelectMany() notation in linq.

   1:  using System.Collections.Generic;
   2:      using System.IO;
   3:   
   4:      using System.Linq;
   5:   
   6:      class Program
   7:      {
   8:          static void Main(string[] args)
   9:          {
  10:              var data = GetAlbums();
  11:              var flattenAlbum = new Album { Id = 0, Name = "Flattenedalbum",  
  12:                  Photos = data.SelectMany(p => p.Photos).ToList() };
  13:          }
  14:   
  15:          private static IEnumerable<Album> GetAlbums()
  16:          {
  17:              var data = new List<Album>
  18:              {
  19:                  new Album
  20:                  {
  21:                      Id = 1, Name = "Album1", Photos = new List<Photo>
  22:                      {
  23:                          new Photo
  24:                          {
  25:                              Id = 1, Name = "Photo1"
  26:                          },
  27:                          new Photo
  28:                          {
  29:                              Id = 2, Name = "Photo2"
  30:                          },
  31:                          new Photo
  32:                          {
  33:                              Id = 3, Name = "Photo3"
  34:                          }
  35:                      }
  36:                  },
  37:                  new Album
  38:                  {
  39:                      Id = 2, Name = "Album2", Photos = new List<Photo>
  40:                      {
  41:                          new Photo
  42:                          {
  43:                              Id = 4, Name = "Photo4"
  44:                          },
  45:                          new Photo
  46:                          {
  47:                              Id = 5, Name = "Photo5"
  48:                          },
  49:                          new Photo
  50:                          {
  51:                              Id = 6, Name = "Photo6"
  52:                          },
  53:                          new Photo
  54:                          {
  55:                              Id = 7, Name = "Photo7"
  56:                          }
  57:                      }
  58:                  }
  59:              };
  60:              return data;
  61:          } 
  62:      }
  63:   
  64:      class Album
  65:      {
  66:          public int Id { get; set; }
  67:          public string Name { get; set; }
  68:          public List<Photo> Photos { get; set; }
  69:      }
  70:   
  71:      class Photo
  72:      {
  73:          public int Id { get; set; }
  74:          public string Name { get; set; }
  75:          public Stream Data { get; set; }
  76:      }


In addition to my previous post, where I was calculating age based on birthdates. If we go the other way around and pretend that we have an age span instead; an age 'from' and 'to' we can easily convert those to represent the datespan that we are interested in the following way.

public class Agespan
{
    public int AgeFrom { get; set; }
    public int AgeTo { get; set; }
 
    public DateTime DateFrom 
    {     get 
        { 
            { 
                return DateTime.Now.Date
                .AddYears(-AgeTo - 1)
                .AddDays(1); 
            }
        } 
    }
    public DateTime DateTo     
    { 
        get 
        { 
            return DateTime.Now.Date
            .AddYears(-AgeFrom)
            .AddHours(23)
            .AddMinutes(59)
            .AddSeconds(59); 
        } 
    }
}


Now we can in instantiate our "Agespan" object and use our dates on the object instead.

var agespan = new Agespan { AgeFrom = 24, AgeTo = 55 };


nov
17

Local variables with λ

af Fasil | Tags: ,

After a long pause I have decided to do all my future posts in English.

In a λ expression, I had a need to call an arbitrary method within my expression.
Now istead of calling the method multiple times within the expression, I had a need to assign the result to a local variable.

Here is the result of how that is achieved.

   1:  var result = mylist.AsEnumerable().Where(x => x 
   2:                      { 
   3:                          var age = GetAge(f.Birthdate); 
   4:                          return age >= search.Age.From && 
   5:                          age <= search.Age.To; 
   6:                      })
   7:                      ).Select(p => new Person 
   8:                          { 
   9:                              Id = p.Id,  
  10:                              Name = p.Name
  11:                          }).ToList();

 

Instead of invoking 'GetAge' method twice, the 'age' variable is declared and the result of the 'GetAge' is assigned to 'age'. The 'age' variable can now be used within the λ expression.



maj
18

JSON.net

af Fasil | Tags: , , ,

Efter en meget lang og sej kamp med den indbyggede json funktionalitet i .net, blev jeg nødt til at kaste håndklædet ved at gøre brug af json.net istedet. Dette lille json hjælpe bibliotek kunne uden problemer serialisere mine komplekse objekter til json.

string json = JsonConvert.SerializeObject(mitobjekt);
return json;

Læs mere her.


dec
25

The Sequence

af Fasil | Tags:

   1:  namespace FibonacciSequence {
   2:      using System;
   3:   
   4:      class Program {
   5:          static void Main() {
   6:              ulong previous = 1;
   7:              ulong current = 1;
   8:              Console.Write(current + "\n");
   9:   
  10:              while (current < 10000000000000000000) {
  11:                  Console.Write(current + "\n");
  12:   
  13:                  ulong next = current + previous;
  14:                  previous = current;
  15:                  current = next;
  16:              }
  17:              Console.ReadKey();
  18:          }       
  19:      }
  20:  }


Hent UML stencils her.

- Åbn Visio 2010 -> Klik på 'Flere figurer' -> Åbn Stencil -> vælg stencil.



Fasil Malik Hayat

Developer
.Net, WCF, Sharepoint, MOSS, Biztalk, JQuery.

linkedin facebook twitter plaxo google+ grooveshark boxee
fasil

Education

fasil

Bachelor of Science (Honours)
De Montfort University, Leicester.

Fag: Java, MVC, Information Strategy, ITIL

Microsoft Certified Technology Specialist

Skills

.net vs2010 - C# sharepoint sql server jquery ubuntu java netbeans


  

Sign in