Exemple #1
0
        static void Main(string[] args)
        {
            PayrollSystem payroll = new PayrollSystem();

            payroll.Add(new Employee {
                Name = "John", Salary = 200
            });
            payroll.Add(new Employee {
                Name = "Mary", Salary = 250, City = "Paris"
            });
            payroll.Add(new Employee {
                Name = "Kate", Salary = 230
            });
            payroll.Add(new Employee {
                Name = "Mike", Salary = 280
            });

            var query = from employee in payroll
                        where employee.Salary > 220
                        select employee.Name;

            query.ToList().ForEach(Console.WriteLine);

            //LINQ to Strings
            string s          = Guid.NewGuid().ToString();
            string digitsOnly =
                new string((from c in s
                            where Char.IsDigit(c)
                            select c).ToArray());

            Console.WriteLine(s + Environment.NewLine + digitsOnly);

            //LINQ to Reflection
            var queryableTypes =
                from asm in AppDomain.CurrentDomain.GetAssemblies()
                from t in asm.GetExportedTypes()
                where t.GetInterfaces().Any(itf =>
                                            itf.IsGenericType &&
                                            ((itf.IsGenericTypeDefinition && itf == typeof(ICollection <>)) ||
                                             (!itf.IsGenericTypeDefinition && itf.GetGenericTypeDefinition() == typeof(ICollection <>)))
                                            ) ||
                typeof(IEnumerable).IsAssignableFrom(t) && t.GetMethods().Any(m => m.Name == "Add")
                select t;

            queryableTypes.ToList().ForEach(t => Console.WriteLine(t.FullName));

            //LINQ to File System
            var largeDllFiles =
                from file in Directory.GetFiles(
                    Environment.GetFolderPath(Environment.SpecialFolder.System))
                let info = new FileInfo(file)
                           where info.Extension == ".dll" && info.Length > 5000000
                           select info.FullName;

            largeDllFiles.ToList().ForEach(Console.WriteLine);

            //LINQ to WCF contracts
            var operations =
                from operation in ContractDescription.GetContract(typeof(IService)).Operations
                where !operation.IsOneWay
                where operation.Faults.Any(f => f.DetailType == typeof(string))
                select new { operation.DeclaringContract.Namespace, operation.SyncMethod.Name };

            operations.ToList().ForEach(Console.WriteLine);
        }
Exemple #2
0
 public static IEnumerable <T> Select <T>(this PayrollSystem payroll, Func <Employee, T> selector)
 {
     return(payroll.employees.Select(selector));
 }