How to convert the output of a Lambda expression to List <T>

I select checked rows from Gridview. For this, I wrote a lambda expression using a dynamic keyword.

var dn = gvLoans.Rows.OfType<dynamic>().Where(s => s.FindControl("chkSelect").Checked == true).Select(s => s.FindControl("lblCD")).ToList();

I want the result of this on the list. Can this be achieved by expanding the query or do I need to write a foreach statement.

+3
source share
2 answers

An impressive break in a comment posted as a response.

List<int> lst = gvRankDetails.Rows
    .OfType<GridViewRow>()
    .Where(s => ((CheckBox)s.FindControl("chkSelect")).Checked) 
    .Select(s => Convert.ToInt32(((Label)s.FindControl("lblCD")).Text))
    .ToList(); 

OfType is necessary because GridViewRowCollection implements IEnumerable, but not IEnumerable<T>.

public class GridViewRowCollection : ICollection, IEnumerable
+4
source
var dn = gvLoans.Rows
  .OfType<dynamic>()
  .Where(s => s.FindControl("chkSelect").Checked == true)
  .Select(s => s.FindControl("lblCD"))
  .Cast<someType>().ToList();

but add .Cast <someType> () before ToList ()

+2
source

All Articles