Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to converte Lambda expression output to List<T>

Tags:

c#

asp.net

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.

like image 323
Ulhas Tuscano Avatar asked Nov 28 '25 03:11

Ulhas Tuscano


1 Answers

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
like image 67
Josh Smeaton Avatar answered Nov 29 '25 18:11

Josh Smeaton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!