DAX Patterns in Linq

We aimed to be able to translate some of the idiomatic query forms from http://www.daxpatterns.com/

Related Distinct count


       [Test]
        public void RelatedDistinctCountPattern()
        {

            var q =
                from sales in _db.InternetSalesSet
                select new
                {
                    Year = sales.RelatedDate.CalendarYear,
                    SoldProduct = sales.RelatedProduct.ProductName.DistinctCount(),
                    SoldProductCategory = sales.RelatedProduct.ProductCategoryName.DistinctCount(sales)
                };
            var res = q.ToList().First(r => r.Year == 2008);
            res.Should().NotBeNull();
            res.SoldProduct.Should().Be(102);
            res.SoldProductCategory.Should().Be(3);
        }


And

Cumulative Total:



        [Test]
        public void CumulativeTotalPattern()
        {

            var q =
                from sales in _db.InternetSalesSet
                select new
                {
                    Year = sales.RelatedDate.CalendarYear,
                    Sum = sales.SalesAmount.Sum(),
                    CumulativeSum =
                        sales.SalesAmount.Sum(
                            sales.ForAll()
                            &&
                            sales.RelatedDate.ForAll(
                                sales.RelatedDate.Date2 <= sales.RelatedDate.Date2.Max()))
                };
            var result = q.ToList();
            var expected = result[0].CumulativeSum + result[1].Sum;
            result[1].CumulativeSum.Should().Be(expected);
            result[1].CumulativeSum.Should().BeGreaterThan(result[0].CumulativeSum);
        }

Last edited Apr 28, 2014 at 11:20 AM by LinqToDax, version 5