Wednesday, March 28, 2012

SelectListItems from a LINQ query using anonymous types

 
                var searchList = from products in productdbConnection.Products
                                 group products by products.Discontinued  into g
                                 select new SelectListItem
                                 {
                                     Text = g.Key.ToString(),
                                     Value = g.Key.ToString()
                                 };
I wanted to show an example of creating SelectListItems from a LINQ query using anonymous types and grouping to grab distinct elements.