Ulhas Tuscano
Ulhas Tuscano

Reputation: 5620

How To update list using lambda expression

I have two lists. lst contains ViewState Data i.e All the records of the gridview & second lstRank contains the list of integer(i.e ID) for only those records which are marked as checked (i.e Gridview contains a columns for checkbox). Now i want to update the lst bool status depending upon integer ID of lstRank. How it can be achieved by lambda expression

 List<Tuple<int, string, bool>> lst = (List<Tuple<int, string,bool>>)ViewState["gvData"];
 List<int> lstRank = gvDetails.Rows.OfType<GridViewRow>().Where(s => ((CheckBox)s.FindControl("chkSelect")).Checked)
                                                                       .Select(s => Convert.ToInt32(((Label)s.FindControl("lblRankCD")).Text)).ToList();

Upvotes: 0

Views: 2308

Answers (1)

Joe White
Joe White

Reputation: 97676

Your question isn't clear, but I'm guessing you want to change lst's contents so that the boolean values are true if the int exists in lstRank, or false if not?

Obviously, tuples are immutable, so if you want to change one of the values, you would have to generate new tuple instances. It's not clear what you mean when you say you specifically want to do this with a lambda expression, but I assume you probably mean that you don't want a solution that involves an explicit loop. So how about this:

lst = lst.Select(oldValues =>
    Tuple.Create(oldValues.Item1,
                 oldValues.Item2,
                 lstRank.Contains(oldValues.Item3))).ToList();

If lstRank is large, you might want to optimize by first building a HashSet out of it, since you'll be doing a lot of Contains calls.

Upvotes: 3

Related Questions