Example #1
0
        private static BasicCollect <DataDemo> GetADemoCollectionForTest()
        {
            var demoData1 = new DataDemo()
            {
                NullInt = null, Property1 = "333"
            };
            var demoData2 = new DataDemo()
            {
                NullInt = 1, Property1 = "444"
            };
            var demoData3 = new DataDemo()
            {
                NullInt = 12, Property1 = "555"
            };
            var demoData4 = new DataDemo()
            {
                NullInt = 123, Property1 = null
            };
            BasicCollect <DataDemo> basicCollect = new BasicCollect <DataDemo>
            {
                demoData1,
                demoData2,
                demoData3,
                demoData4
            };

            return(basicCollect);
        }
Example #2
0
        private static BasicCollect <DataDemo2> GetADemo2CollectionForTest()
        {
            var demoData1 = new DataDemo2()
            {
                IntData = 0, StringData = "333"
            };
            var demoData2 = new DataDemo2()
            {
                IntData = 1, StringData = "444"
            };
            var demoData3 = new DataDemo2()
            {
                IntData = 12, StringData = "555"
            };
            var demoData4 = new DataDemo2()
            {
                IntData = 133, StringData = "222"
            };
            BasicCollect <DataDemo2> basicCollect = new BasicCollect <DataDemo2>
            {
                demoData1,
                demoData2,
                demoData3,
                demoData4
            };

            return(basicCollect);
        }
Example #3
0
        public static void DoALinQDemo()
        {
            BasicCollect <DataDemo> dataDemoCollection = GetADemoCollectionForTest();
            var dataFromLinQ = from demo in dataDemoCollection
                               where demo.NullInt != null
                               select demo;

            foreach (DataDemo data in dataFromLinQ)
            {
                Console.WriteLine("demount: {0}, demopro1: {1}", data.NullInt ?? 9999, data.Property1 ?? "null");
            }
            var dataFromLinQ2 = Enumerable.Where(dataDemoCollection, demo => demo.NullInt != null && demo.Property1 == "444")
                                .Where(demo => demo.Property2 != null);

            foreach (DataDemo data in dataFromLinQ2)
            {
                Console.WriteLine("demount: {0}, demopro1: {1}", data.NullInt ?? 9999, data.Property1 ?? "null");
            }
        }
Example #4
0
        public static void DoALinQJoinDemo()
        {
            BasicCollect <DataDemo>  dataDemoCollection1 = GetADemoCollectionForTest();
            BasicCollect <DataDemo2> dataDemoCollection2 = GetADemo2CollectionForTest();
            var dataFromLinq = from demo in dataDemoCollection1
                               join demo2 in dataDemoCollection2
                               on demo.NullInt equals demo2.IntData

                               where demo.Property1 == demo2.StringData
                               select new
            {
                IntDataTemp    = demo2.IntData,
                StringDataTemp = demo.Property2
            };
            var dataFromLambda = dataDemoCollection1
                                 .Join(dataDemoCollection2, i => i.NullInt, j => j.IntData, (i, j) => new { demo = i, demo2 = j })
                                 .Where(d => d.demo.Property1 == d.demo2.StringData)
                                 .Select(j => new { IntDataTemp = j.demo2.IntData, StringDataTemp = j.demo.Property2 });
            Dictionary <int, string> dic  = dataFromLinq.ToDictionary(k => k.IntDataTemp, v => v.StringDataTemp);
            Dictionary <int, string> dic2 = dataFromLambda.ToDictionary(k => k.IntDataTemp, v => v.StringDataTemp);
        }
Example #5
0
        public static void DoALinQOrderByDemo()
        {
            BasicCollect <DataDemo2> dataDemoCollection = GetADemo2CollectionForTest();
            var dataForLinq = from demo in dataDemoCollection
                              orderby demo.IntData
                              select new
            {
                demo.IntData,
                StringData1 = demo.StringData,
                demo.StringData2
            };
            var dataForLambda = dataDemoCollection.OrderBy(k => k.IntData).Select(j => new { j.IntData, StringData1 = j.StringData, j.StringData2 });

            foreach (var demo in dataForLinq)
            {
                Console.WriteLine("IntData: {0}, StringData1: {1}, StringData2: {2}", demo.IntData, demo.StringData1, demo.StringData2);
            }
            foreach (var demo in dataForLambda)
            {
                Console.WriteLine("`IntData: {0}, StringData1: {1}, StringData2: {2}", demo.IntData, demo.StringData1, demo.StringData2);
            }
        }