Need help converting c # foreach loop to lambda

Is it possible to implement the following loop using IQueryable, IEnumerable, or lambda expressions using linq

private bool functionName(int r, int c)
{
    foreach (S s in sList)
    {
        if (s.L.R == r && s.L.C == c)
        {
            return true;
        }
    }

    return false;
}

if so how?

+5
source share
4 answers

Try:

private bool functionName(int r, int c)
{
    return sList.Any(s => s.L.R == r && s.L.C == c);
}

Any extension method in Linq is applied to the IEnumerable sequence (which may be an example) and returns true if any of the elements in the sequence returns true for the given predicate (in this case, the lambda function s => s.L.R == r && s.L.C == c).

+6
source

sort of:

return sList.Any(s => s.L.R == r && s.L.C == c);
+6
source

(s.L.R??), , , , 100 :

return sList.Any(s => s.L.R == r && s.L.C == c);

/e: , , , . .

0

private bool functionName(int r,int c)
{
  var ret = from s in sList where s.L.R==r&&s.L.C==c select s;
  return ret.Count()>0;
}
-1
source

All Articles