asp.net core LINQ where clause

Abdullah Durrani 41 Reputation points
2021-09-12T12:42:20.31+00:00

I'm new to asp.net and learning from pluralsight and instructor uses this LINQ query which I can't understand .
public IEnumerable<Restaurant> GetRestaurantsByName(string name = null)
{
return from r in restaurants
where string.IsNullOrEmpty(name) || r.Name.StartsWith(name)
orderby r.Name
select r;
}
I can't understand the where part. What does it do especially the first part string.IsNullOrEmpty(name)

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,308 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,386 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yiyi You - MSFT 521 Reputation points
    2021-09-13T05:32:46.083+00:00

    Hi,AbdullahDurrani-7930,

    GetRestaurantsByName gets restaurants with name.Firstly you need to check if name is empty or null with string.IsNullOrEmpty(name),if it is null or empty,you only need to return all restaurants.If it is not null or empty,you need to get restaurants with name.So there is a || between string.IsNullOrEmpty(name) and r.Name.StartsWith(name).Only when name is not null or empty,it will check r.Name with r.Name.StartsWith(name).

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    YiyiYou

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful