示例#1
0
        public void TestToDictionaryWhenThrowingExceptions()
        {
            List <Employee> employees = null;

            Func <Employee, int>    myKeyFunc     = (x) => x.ID;
            Func <Employee, string> myElementFunc = (x) => x.FirstName;

            Assert.Throws <ArgumentNullException>(() => LINQFunctions.ToDictionary(employees, p => myKeyFunc(p), z => myElementFunc(z)));

            employees = new List <Employee>();

            var firstEmployee = new Employee()
            {
                ID = 1, FirstName = "Andrei", LastName = "Popescu"
            };
            var secondEmployee = new Employee()
            {
                ID = 1, FirstName = "Mihai", LastName = "Andreescu"
            };

            employees.Add(firstEmployee);
            employees.Add(secondEmployee);

            Assert.Throws <ArgumentException>(() => LINQFunctions.ToDictionary(employees, p => myKeyFunc(p), z => myElementFunc(z)));
        }
示例#2
0
        public void TestToDictionary()
        {
            var employees = Employee.GetEmployees();

            Func <Employee, int>    myKeyFunc     = (x) => x.ID;
            Func <Employee, string> myElementFunc = (x) => x.FirstName;

            var dictionary = LINQFunctions.ToDictionary(employees, p => myKeyFunc(p), z => myElementFunc(z));

            var kvp1 = new KeyValuePair <int, string>(104, "Anurag");
            var kvp2 = new KeyValuePair <int, string>(105, "Sambit");
            var kvp3 = new KeyValuePair <int, string>(106, "Sushanta");
            var kvp4 = new KeyValuePair <int, string>(101, "Preety");
            var kvp5 = new KeyValuePair <int, string>(103, "Hina");
            var kvp6 = new KeyValuePair <int, string>(102, "Priyanka");

            var kvp7 = new KeyValuePair <int, string>(109, "Andrei");

            Assert.True(dictionary.Contains(kvp1));
            Assert.True(dictionary.Contains(kvp2));
            Assert.True(dictionary.Contains(kvp3));
            Assert.True(dictionary.Contains(kvp4));
            Assert.True(dictionary.Contains(kvp5));
            Assert.True(dictionary.Contains(kvp6));

            Assert.False(dictionary.Contains(kvp7));
        }