How can I convert below code in LINQ
foreach (RepeaterItem ri in rptNews.Items)
{
HiddenField hdnUserId = (HiddenField)ri.FindControl("hdnId");
int userId = Users.Current.UserId;
if (Convert.ToInt32(hdnUserId.Value) != userId)
{
((ImageButton)ri.FindControl("img1")).Visible = false;
((ImageButton)ri.FindControl("img2")).Visible = false;
}
}
Also please guide me how can I learn to translate this kind of code to linq.
Linq is intended to select a subset of data not just iterate over a collection. In your example you are not selecting anything, you are simply looping through a collection. Linq is not the correct tool for what you are trying to achieve.
Regarding your comment that Linq is faster, a linq query will eventually boil down to a series of
forandifstatements once the compiler has worked its magic, so I doubt it would be any faster than youforeacheven if you could use it without aselectclause.