Example #1
0
 static void Main(string[] args)
 {
     using (var db = new NutshellContextDataContext())
     {
         var t = Enumerable.Empty<int>();
     }
     WhereSample();
     WhereSample2();
     TakeWhileSample();
     Projecting();
     Hierarchy();
     SubQuery();
     SelectManySample();
     SelectManySample2();
     JoinSample();
 }
Example #2
0
        static void ProjectionFromDB()
        {
            XDocument doc;

            using (var db = new NutshellContextDataContext())
            {
                var custList = (from c in db.Customers
                                select new { Name = c.Name, ID = c.ID, Count = c.Purchases.Count }).ToList();

                doc = new XDocument(new XStreamingElement("Customers",
                                                          from c in custList
                                                          select new XElement("Customer", new XAttribute("id", c.ID),
                                                                              new XElement("Name", c.Name),
                                                                              new XElement("Buys", c.Count))
                                                          ));

                Console.WriteLine(doc.ToString());
            }
        }
Example #3
0
        private static void JoinSample()
        {
            using (var db = new NutshellContextDataContext())
            {
                var customers = db.Customers.ToArray();
                var purchases = db.Purchases.ToArray();
                var slowQuery =
                    from c in customers
                    from p in purchases
                    where c.ID == p.CustomerID
                    select c.Name + " bought a " + p.Description;

                var fastQuery =
                    from c in customers
                    join p in purchases on c.ID equals p.CustomerID
                    select c.Name + " bought a " + p.Description;


            }
        }
Example #4
0
        private static void SubQuery()
        {
            using (var db = new NutshellContextDataContext())
            {
                // kind of left outer join
                var queryLOJ =
                    from c in db.Customers
                    select new
                    {
                        c.Name,
                        Purchases = from p in c.Purchases
                                    where p.CustomerID == c.ID && p.Price > 600
                                    select new { p.Description, p.Price }
                    };

                // kind of inner join (sql becomes left outer join with exists)
                var queryRIJ =
                    from c in db.Customers
                    let hpur = from p in c.Purchases where p.Price > 600 select new { p.Description, p.Price }
                    where hpur.Any()
                    select new { c.Name, Purchases = hpur };

                //var queryJKL =
                //    from p in db.Purchases
                //    group g by p.CustomerID as ff

                    


                foreach (var cname in queryLOJ)
                {
                    Console.WriteLine($"Customer: {cname.Name}");
                    foreach (var pur in cname.Purchases)
                    {
                        Console.WriteLine($"\t{pur.Price}");
                    }
                }
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            Expression <Func <Customer, bool> > first = (c) => c.ID == 1; // ->LambdaExpression -> Expression

            Console.WriteLine(first.Body.NodeType);
            Console.WriteLine(((BinaryExpression)first.Body).Left);

            using (var db = new NutshellContextDataContext())
            {
                var query = from c in db.Customers
                            where c.Name.Contains("a")
                            orderby c.Name.Length
                            select c.Name.ToUpper();

                System.Console.WriteLine(query);
                System.Console.WriteLine("Result: " + TS(query));

                DataLoadOptions opt = new DataLoadOptions();
                //opt.AssociateWith<Customer>(c => c.Purchases.Where(p => p.Price > 900));
                opt.LoadWith <Customer>(c => c.Purchases); //force eager loading
                db.LoadOptions = opt;
                var q2 = from c in db.Customers
                         select
                         from p in c.Purchases
                         select new { c.Name, p.Price };

                //System.Console.WriteLine(q2);

                foreach (var c1 in q2)
                {
                    foreach (var namePrice in c1)
                    {
                        Console.WriteLine(namePrice);
                    }
                    //Console.WriteLine($"{namePrice.Name} +spent {namePrice.Price}");
                }
            }
        }