Thursday, March 1, 2012

projection in LINQ

if you are not projecting the selected object into a new object then you don't need select
consider:

this.listBox1.Items.Clear();
            Type t = this.GetType();
            PropertyInfo[] p = t.GetProperties();
            IEnumerable<PropertyInfo> names = p.Where(cust => cust.Name.StartsWith("S"));
            foreach (PropertyInfo s in names)
            {
                this.listBox1.Items.Add(s.Name);
                       }

here we are returning a property info object and there we need not the select

but here:

this.listBox1.Items.Clear();
            Type t = this.GetType();
            PropertyInfo[] p = t.GetProperties();
            IEnumerable<string> names = p.Where(cust => cust.Name.StartsWith("S")).Select(cust => cust.Name);    //from nms in p select nms.Name;
            foreach (string s in names)
            {
                this.listBox1.Items.Add(s);
            }
we need the select because we are changing the projected type

No comments:

Post a Comment