开发者

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();


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜