Click here to Skip to main content
15,895,813 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.5K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Signum.Engine;
using Signum.Entities;
using Signum.Utilities;

namespace Signum.Test.LinqProvider
{
    /// <summary>
    /// Summary description for SelectManyTest
    /// </summary>
    [TestClass]
    public class SelectManyTest
    {
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            Starter.StartAndLoad();
        }

        [TestInitialize]
        public void Initialize()
        {
            Connection.CurrentLog = new DebugTextWriter();
        }

        [TestMethod]
        public void SelectMany()
        {
            var artistsInBands = Database.Query<BandDN>().SelectMany(b => b.Members).Select(a => new { Artist = a.ToLite() }).ToList();
        }


        [TestMethod]
        public void SelectManyIndex()
        {
            var artistsInBands = Database.Query<BandDN>().SelectMany((b, i) => b.Members.Select(m => new { Artist = m.ToLite(), i })).ToList();
        }

        [TestMethod]
        public void SelectMany2()
        {
            var artistsInBands = Database.Query<BandDN>().SelectMany(b => b.Members, (b, a) => new { Artist = a.ToLite(), Band = b.ToLite() }).ToList();
        }

        [TestMethod]
        public void SelectMany2Index()
        {
            var artistsInBands = Database.Query<BandDN>().SelectMany((b, i) => b.Members.Select(m => new { Artist = m.ToLite(), i }), (b, a) => new { a.Artist, a.i, Band = b.ToLite() }).ToList();
        }

        [TestMethod]
        public void SelectManyWhere1()
        {
            var artistsInBands = Database.Query<BandDN>().SelectMany(b => b.Members.Where(a => a.IsMale)).Select(a => new { Artist = a.ToLite() }).ToList();
        }

        [TestMethod]
        public void SelectManyWhere2()
        {
            var artistsInBands = Database.Query<BandDN>().Where(b => b.LastAward != null).SelectMany(b => b.Members.Where(a => a.IsMale)).Select(a => a.ToLite()).ToList();
        }

        [TestMethod]
        public void SelectManyEmbedded()
        {
            var artistsInBands = Database.Query<AlbumDN>().SelectMany(a => a.Songs, (a, s) => s.Name).ToList();
        }

        [TestMethod]
        public void SelectManyLazy()
        {
            var artistsInBands = Database.Query<ArtistDN>().SelectMany(a=>a.Friends).ToList();
        }

        [TestMethod]
        public void SelectManyDefaultIfEmpty()
        {
            var artistsInBands = Database.Query<BandDN>().SelectMany(b => b.Members.DefaultIfEmpty()).Select(a => new { Artist = a.ToLite() }).ToList();
        }

        [TestMethod]
        public void SelectManyOverload()
        {
            var artistsInBands = (from a1 in Database.Query<ArtistDN>()
                                  from a in a1.Friends
                                  select new
                                  {
                                      Artist = a1.ToLite(),
                                      Friend = a,
                                  }).ToList();
        }


        [TestMethod]
        public void SelectManyDefaultIfEmptyTwo()
        {
            var artistsInBands = (from a1 in Database.Query<ArtistDN>()
                                  from a in a1.Friends.DefaultIfEmpty()
                                  select new
                                  {
                                      Artist = a1.ToLite(),
                                      Friend = a,
                                  }).ToList();
        }

        [TestMethod]
        public void SelectManyDefaultIfEmptyNotNull()
        {
            var artistsInBands = (from a1 in Database.Query<ArtistDN>()
                                  from a in a1.Friends.DefaultIfEmpty()
                                  select new
                                  {
                                      Artist = a1.ToLite(),
                                      Friend = a,
                                      HasFriend = a != null
                                  }).ToList();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions