// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; namespace E { public class Data { public int Iv { get; set; } public string Sv { get; set; } } public class Program { public static List Filter(List lst, int? iv, string sv) { IEnumerable res = lst; if(iv != null) { res = res.Where(o => o.Iv == (int)iv); } if(sv != null) { res = res.Where(o => o.Sv == sv); } return res.ToList(); } public static void Dump(List lst) { foreach(Data o in lst) { Console.WriteLine("{0} {1}", o.Iv, o.Sv); } } public static void Test(List lst, int? iv, string sv) { Console.WriteLine("Filter: {0} {1}", iv, sv); Dump(Filter(lst, iv, sv)); } public static void Main(string[] args) { List lst = new List { new Data { Iv = 1, Sv = "A" }, new Data { Iv = 2, Sv = "BB" }, new Data { Iv = 3, Sv = "CCC" }, new Data { Iv = 3, Sv = "A" } }; Test(lst, 1, null); Test(lst, 3, null); Test(lst, null, "A"); Test(lst, null, "BB"); Test(lst, 3, "CCC"); } } }