开发者

Is it right a LINQ query to check whether atleast one entity exists in string collection with a matching criteria (rownum<=1)

开发者 https://www.devze.com 2023-02-07 11:16 出处:网络
I was trying to do something similar to ForEach in LINQ, to find in a string collection whether atleast a single entity is matching with the matching string.

I was trying to do something similar to ForEach in LINQ, to find in a string collection whether atleast a single entity is matching with the matching string.

I was trying to create a query something similar to Rownum<=1 in Oracle.

Please suggest if LINQ code is a good approach.

ForEach:

bool check(string matchcriteria)
{
  foreach ( string key in Keys)
                if ( key.ToLower() == matchcriteria.ToLower()) return true;
  return false;
}

LINQ;

bool check(string matchcriteria)
{
 return (from  key in Keys
                    开发者_运维技巧       where  key.ToLower() == matchcriteria.ToLower() select 10).FirstOrDefault()==10;
}

thanks Srinivas Pv


The LINQ Any method would be better suited.

bool check(string matchcriteria)
{
    return Keys.Any(key => key.ToLower() == matchcriteria.ToLower());
}
0

精彩评论

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