0

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.

Ray
  • 45,695
  • 27
  • 126
  • 169
Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206
  • 3
    Why do you want to "translate" this code into LINQ? It looks fine as it is. LINQ is not the solution to every problem. – Frédéric Hamidi Jan 13 '12 at 10:20
  • I read that Linq is faster and I have so many items in repeater. So I thought LINQ can work better than traditional foreach loop. I am not sure because I am new to linq :-( – Zerotoinfinity Jan 13 '12 at 10:21
  • 1
    Whether or not LINQ is faster is irrelevant if what you have now is readable and fast enough. – George Duckett Jan 13 '12 at 10:23
  • 3
    Given that LINQ is not intended for causing side-effects, you really should loop somewhere. – spender Jan 13 '12 at 10:23
  • I am confused now because of my limited knowledge in LINQ. Does that mean that foreach is best in this case ? – Zerotoinfinity Jan 13 '12 at 10:25
  • 1
    @Zerotoinfinite LINQ is NOT faster than foreach or for loops. http://stackoverflow.com/questions/3156059/linq-statement-faster-than-foreach-loop – giammin Jan 13 '12 at 10:30
  • 1
    @Zerotoinfinite, `foreach` gets the job done. You could refactor it into a LINQ query, apply `ToList()` to it and pass a lambda to the `ForEach()` method of the resulting list, but the end result will probably perform the same and only be less readable. – Frédéric Hamidi Jan 13 '12 at 10:30
  • 1
    LINQ may hide the loop a little bit but would certainly not eliminate it. – Serge Wautier Jan 13 '12 at 10:30

3 Answers3

3

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 for and if statements once the compiler has worked its magic, so I doubt it would be any faster than you foreach even if you could use it without a select clause.

Greg B
  • 14,597
  • 18
  • 87
  • 141
1

I found the 101 LINQ Samples guide on MSDN very useful when learning LINQ.

Have a look at the examples and have a go at re-writing the above query yourself - it's the only way to learn.

The Ed R
  • 316
  • 2
  • 6
1

Since you asked for it, find it below:

foreach (RepeaterItem ri in from RepeaterItem ri in rptNews.Items let hdnUserId = (HiddenField)ri.FindControl("hdnId") let userId = Users.Current.UserId where Convert.ToInt32(hdnUserId.Value) != userId select ri)
            {
                ((ImageButton)ri.FindControl("img1")).Visible = false; 
                ((ImageButton)ri.FindControl("img2")).Visible = false;
            } 
Rajesh
  • 7,766
  • 5
  • 22
  • 35