Example #1
0
        public void ShouldLeftJoinAnonimous()
        {
            var items = new[]
            {
                new {Id = 1, Name = "Item1"},
                new {Id = 2, Name = "Item2"},
                new {Id = 3, Name = "Item3"},
                new {Id = 4, Name = "Item4"},
                new {Id = 5, Name = "Item5"}
            };

            var groups = new[]
            {
                new {Id = 1, ItemId = 1, Name = "Group1"},
                new {Id = 2, ItemId = 1, Name = "Group2"},
                new {Id = 3, ItemId = 2, Name = "Group3"}
            };

            var itemGroups = items.GroupJoin(groups, x => x.Id, y => y.ItemId, (x, y) => new {Item = x, Groups = y})
                .SelectMany(result => result.Groups.DefaultIfEmpty(), (result, group) => new
                {
                    Item = result.Item.Name,
                    Group = (group == null) ? "<none>" : group.Name
                }).ToList();
            var item2 = itemGroups.First(x => x.Item == "Item2");
            Assert.AreEqual("Group3", item2.Group);

            var item5 = itemGroups.First(x => x.Item == "Item5");
            Assert.AreEqual("<none>", item5.Group);
        }
Example #2
0
        public void ReferenceOuter_ReferenceInner_ValueKey_ReferenceResult()
        {
            string[] outer = new [] {"0", "1", "2", "3", "4", "5", "6"};
            string[] inner = new [] {"0", "1", "2", "3", "4", "5", "6"};

            IEnumerable<string> result = outer.GroupJoin<string, string, int, string> (inner: inner, outerKeySelector: it => int.Parse (it), innerKeySelector: it => int.Parse (it), resultSelector: (o, i) => o);
            Assert.That (result.Count (), Is.EqualTo (result.Count ()));
            for (int i = 0; i < outer.Length; i++) {
                Assert.That (outer [i], Is.EqualTo (result.ElementAt (i)));
            }
        }
Example #3
0
        public void SimpleGroupJoin()
        {
            var outer = new[] { "paul", "adam", "alex", "victor" };
            var inner = new[] { "apple", "banana", "orange", "pineapple", "pear" };

            var result = outer.GroupJoin(inner,
                person => person[0],
                fruit => fruit[0],
                (person, fruits) => person + ":" + string.Join(",", fruits));
            result.AssertSequenceEqual("paul:pineapple,pear", "adam:apple", "alex:apple", "victor:");
        }
Example #4
0
        public void ReferenceOuter_ReferenceInner_ReferenceKey_ReferenceResult_Comparer()
        {
            IEqualityComparer<string> comparer = EqualityComparer<string>.Default;
            string[] outer = new [] {"0", "1", "2", "3", "4", "5", "6"};
            string[] inner = new [] {"0", "1", "2", "3", "4", "5", "6"};

            IEnumerable<string> result = outer.GroupJoin<string, string, string, string> (inner: inner, outerKeySelector: it => it, innerKeySelector: it => it, resultSelector: (o, i) => o, comparer: comparer);
            Assert.That (result.Count (), Is.EqualTo (result.Count ()));
            for (int i = 0; i < outer.Length; i++) {
                Assert.That (outer [i], Is.EqualTo (result.ElementAt (i)));
            }
        }
Example #5
0
        protected override void OnStart(string[] args)
        {
            _timer = new Timer(_ =>
            {
                var trackingQuery = new [] {"#MH370", "Tony Abbot", "Obama", "Putin", "Ukraina"};

                var allAudit = _auditService.GetAll();

                var commands = trackingQuery.GroupJoin(allAudit, x => x, a => a.Keyword, (x,a) => new NewsSearchCommand
                {
                    LatestArticleDate = a.DefaultIfEmpty().FirstOrDefault() != null
                                      ? a.DefaultIfEmpty().First().LastArticleDate
                                      : DateTime.MinValue,
                    Query = x
                }).ToList();
                commands.ForEach(c => _commandSender.Send(c));
            });
            _timer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromHours(20));
        }
Example #6
0
		public void group_join()
		{
			var values = new[] { 0, 2 };
			var fixtures = CreateSequentialFixtures(3);

			var sut = CreateDynamicList(values);

			var expected = values.GroupJoin(fixtures, x => x, x => x.Value, (x, i) => new
			{
				JoinValue = x,
				Fixtures = i.Select(f => f.Id)
			}).ToList();

			var result = sut.GroupJoin(fixtures, x => x, x => x.Value, (x, i) => new
			{
				JoinValue = x,
				Fixtures = i.Select(f => f.Id)
			}).ToList();

			Assert.Equal(expected.Count(), result.Count());

			for (int i = 0; i < expected.Count; i++)
			{
				Assert.Equal(expected[i].Fixtures, result[i].Fixtures);
				Assert.Equal(expected[i].JoinValue, result[i].JoinValue);
			}
		}
        static void Main(string[] args)
        {
            List<Student> listOfStudents = new List<Student>();
            listOfStudents.Add(new Student("Dimitar", "Dimitrov", 1021006, "02-123123123", "[email protected]bv.bg", new List<int> { 2, 3, 4, 5, 6, 6, 6 }, 1));
            listOfStudents.Add(new Student("Georgi", "Atanasov", 1021231, "056-123123123", "[email protected]", new List<int> { 2, 3, 4, 5, 6, 6, 6 }, 2));
            listOfStudents.Add(new Student("Ivan", "Stoimenov", 1021231, "056-123123123", "[email protected]", new List<int> { 2, 3, 4, 5, 6, 6, 6 }, 2));
            listOfStudents.Add(new Student("Atanas", "Dimitrov", 1021006, "02-123123123", "[email protected]", new List<int> { 2, 3, 4, 5, 2, 6, 6 }, 2));
            listOfStudents.Add(new Student("Strahil", "Stoimenov", 1021231, "02-123123123", "[email protected]", new List<int> { 2, 3, 4, 5, 6, 6,
            6 }, 1));
            var sortedStudents =
                from student in listOfStudents
                where student.GroupNumber == 2
                orderby student.FirstName
                select student;

            foreach (var item in sortedStudents)
            {
                Console.WriteLine(item.FirstName + " " + item.LastName + " " + item.groupNumber);
            }
            //10
            var sortedStudentsUsingLambdaFunctions = listOfStudents.Where(x => x.GroupNumber == 2).OrderBy(x => x.FirstName);

            foreach (var item in sortedStudentsUsingLambdaFunctions)
            {
                Console.WriteLine(item.FirstName + " " + item.LastName + " " + item.groupNumber);
            }
            //11
            var studentsByAbvBg =
                from student in listOfStudents
                where student.Email.Substring(student.Email.Length - 6, 6) == "abv.bg"
                select student;

            foreach (var item in studentsByAbvBg)
            {
                Console.WriteLine(item.FirstName + " " + item.LastName + " " + item.Email);
            }
            //12
            var studentsBySofiaTel =
                from student in listOfStudents
                where student.Tel.Substring(0, 3) == "02-"
                select student;

            foreach (var item in studentsBySofiaTel)
            {
                Console.WriteLine(item.FirstName + " " + item.LastName + " " + item.Tel);
            }
            // 13
            var studentsWithHighestGrades =
                from student in listOfStudents
                where student.Grades.Contains(6)
                select new {FullName = student.FirstName + " " + student.LastName, Marks = student.Grades };

            foreach (var item in studentsWithHighestGrades)
            {
                Console.WriteLine(item.FullName);
                foreach (var grade in item.Marks)
                {
                    Console.WriteLine(grade.ToString());
                }
            }
            // 14
            var studentsWithTwoLowGrades = listOfStudents.Where(x => x.Grades.Count(m => m == (dynamic)2) == (dynamic)2);

            foreach (var item in studentsWithTwoLowGrades)
            {
                Console.WriteLine("{0}", item.FirstName);
                foreach (var grade in item.Grades)
                {
                    Console.WriteLine("{0}", grade);
                }
            }
            //15
            var enrolledIn2006 = listOfStudents.Where(student => student.FacultyNumber.ToString().Substring(5, 2) == "06");

            foreach (var item in enrolledIn2006)
            {
                Console.WriteLine();
                foreach (var grade in item.Grades)
                {
                    Console.Write(grade.ToString() + " ");
                }
            }
            Console.WriteLine();
            //16
            var listOfGroups = new[]
            {
                new Group(1, "Mathematics"),
                new Group(2, "Philosophy")
            };

            var mathematicians =
                from student in listOfStudents
                join departament in listOfGroups on student.GroupNumber equals departament.GroupNumber
                where departament.DepartamentName == "Mathematics"
                select student;

            foreach (var item in mathematicians)
            {
                Console.WriteLine("{0} {1}", item.FirstName, item.LastName);
            }
            //18
            var newListOfGroups = new[]
            {
                new Group("Troll",1),
                new Group("Goblin",2)
            };

            var newListOfStudents = new[]
            {
                new Student("Tihomir", "Tihomirov", 1),
                new Student("Dimitar", "Dimitrov", 1),
                new Student("Stefan", "Stefanoc", 2),
                new Student("Boian", "Boianov", 2)
            };

            var groupedByGroupName =
                from student in newListOfStudents
                join someGroup in newListOfGroups on student.GroupNumber equals someGroup.GroupNumber
                select new {Name = student.FirstName + " " + student.LastName, someGroup.GroupName };

            foreach (var item in groupedByGroupName)
            {
                Console.WriteLine(item.Name + " " + item.GroupName);
            }
            //19
            var groupedByUsingLambda = newListOfStudents.GroupJoin
                (newListOfGroups,
                student => student.GroupNumber,
                someGroup => someGroup.GroupNumber,
                (student, someGroups) =>
                    new { FirstName = student.FirstName + " " + student.LastName,
                        GroupNumber = someGroups.Select(someGroup => someGroup.GroupName) });
            foreach (var item in groupedByUsingLambda)
            {
                Console.WriteLine("{0}", item.FirstName);
                foreach (var obj in item.GroupNumber)
                {
                    Console.WriteLine(" {0}", obj);
                }
            }
        }
Example #8
0
        public void ValueOuter_ValueInner_ValueKey_ValueResult_Comparer()
        {
            IEqualityComparer<int> comparer = EqualityComparer<int>.Default;
            int[] outer = new [] {0, 1, 2, 3, 4, 5, 6};
            int[] inner = new [] {0, 1, 2, 3, 4, 5, 6};

            IEnumerable<int> result = outer.GroupJoin<int, int, int, int> (inner: inner, outerKeySelector: it => it, innerKeySelector: it => it, resultSelector: (o, i) => o, comparer: comparer);
            Assert.That (result.Count (), Is.EqualTo (result.Count ()));
            for (int i = 0; i < outer.Length; i++) {
                Assert.That (outer [i], Is.EqualTo (result.ElementAt (i)));
            }
        }
Example #9
0
        public void ValueOuter_ValueInner_ValueKey_ReferenceResult()
        {
            int[] outer = new [] {0, 1, 2, 3, 4, 5, 6};
            int[] inner = new [] {0, 1, 2, 3, 4, 5, 6};

            IEnumerable<string> result = outer.GroupJoin<int, int, int, string> (inner: inner, outerKeySelector: it => it, innerKeySelector: it => it, resultSelector: (o, i) => o.ToString ());
            Assert.That (result.Count (), Is.EqualTo (result.Count ()));
            for (int i = 0; i < outer.Length; i++) {
                string value = outer [i].ToString ();
                Assert.That (value, Is.EqualTo (result.ElementAt (i)));
            }
        }
Example #10
0
        public void ValueOuter_ReferenceInner_ValueKey_ReferenceResult_Comparer()
        {
            IEqualityComparer<int> comparer = EqualityComparer<int>.Default;
            int[] outer = new [] {0, 1, 2, 3, 4, 5, 6};
            string[] inner = new [] {"0", "1", "2", "3", "4", "5", "6"};

            IEnumerable<string> result = outer.GroupJoin<int, string, int, string> (inner: inner, outerKeySelector: it => it, innerKeySelector: it => int.Parse (it), resultSelector: (o, i) => o.ToString (), comparer: comparer);

            Assert.That (result.Count (), Is.EqualTo (result.Count ()));
            for (int i = 0; i < outer.Length; i++) {
                string value = outer [i].ToString ();
                Assert.That (value, Is.EqualTo (result.ElementAt (i)));
            }
        }
Example #11
0
        public void ValueOuter_ReferenceInner_ReferenceKey_ValueResult()
        {
            int[] outer = new [] {0, 1, 2, 3, 4, 5, 6};
            string[] inner = new [] {"0", "1", "2", "3", "4", "5", "6"};

            IEnumerable<int> result = outer.GroupJoin<int, string, string, int> (inner: inner, outerKeySelector: it => it.ToString (), innerKeySelector: it => it, resultSelector: (o, i) => o);

            Assert.That (result.Count (), Is.EqualTo (result.Count ()));
            for (int i = 0; i < outer.Length; i++) {
                Assert.That (outer [i], Is.EqualTo (result.ElementAt (i)));
            }
        }
Example #12
0
        public void ReferenceOuter_ValueInner_ValueKey_ValueResult()
        {
            string[] outer = new [] {"0", "1", "2", "3", "4", "5", "6"};
            int[] inner = new [] {0, 1, 2, 3, 4, 5, 6};

            IEnumerable<int> result = outer.GroupJoin<string, int, int, int> (inner: inner, outerKeySelector: it => int.Parse (it), innerKeySelector: it => it, resultSelector: (o, i) => int.Parse (o));
            Assert.That (result.Count (), Is.EqualTo (result.Count ()));
            for (int i = 0; i < outer.Length; i++) {
                int value = int.Parse (outer [i]);
                Assert.That (value, Is.EqualTo (result.ElementAt (i)));
            }
        }