Ulhas Tuscano
Ulhas Tuscano

Reputation: 5620

How to converte Lambda expression output to List<T>

I am selecting the checked rows from Gridview. To achieve this i have written a lambda expression using dynamic keyword.

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

I want the output of this in List. Can it be achieved by extending the query or i have to write foreach statement.

Upvotes: 3

Views: 2447

Answers (2)

Josh Smeaton
Josh Smeaton

Reputation: 48730

Blatant rip of the comment posted as an answer.

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, as GridViewRowCollection implements IEnumerable but not IEnumerable<T>.

public class GridViewRowCollection : ICollection, IEnumerable

Upvotes: 4

Bonshington
Bonshington

Reputation: 4032

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

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

Upvotes: 2

Related Questions