Wednesday, July 3, 2013

Select multiple fields in Generic List using LINQ query

The below code will explain the simple LINQ Query with multiple select column name.  Here I used to filter records from lstValue with ID have “100”.  The filter Linq Query will return the ID and Name values only. It will omit the Class in the result.

 public class KeyValue
        public string ID { getset; }
        public string Name { getset; }
        public string Class { getset; }

//Declaration of KeyValue Class
 public List<KeyValuelstValuegetset; }

// Filter in LINQ Query
var Result = lstValue.Select(c => new { c.ID, c. Name, c.Class });

            var result = from a in lstValue
                         where a.ID == "100"
                         select new { a.ID, a.Name };
Assign the Generic List values to grid view.
//grdDetails – Datagrid (Assign the value to Grid)
grdDetails.DataSource = result.ToList();

No comments: