question

AbdullahDurrani-7930 avatar image
0 Votes"
AbdullahDurrani-7930 asked AbdullahDurrani-7930 commented

asp.net core LINQ where clause

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)

dotnet-aspnet-core-razordotnet-aspnet-webpages
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YiyiYou-MSFT avatar image
1 Vote"
YiyiYou-MSFT answered AbdullahDurrani-7930 commented

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


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So if the first part is true it skips the next part after || ??

0 Votes 0 ·
Bruce-SqlWork avatar image Bruce-SqlWork AbdullahDurrani-7930 ·

yes.

sqlserver supports expression short-circuits. with an "or" expression, it skips on the first true, with an "and" expression, skips on the first false.

1 Vote 1 ·
YiyiYou-MSFT avatar image YiyiYou-MSFT AbdullahDurrani-7930 ·

Hi,AbdullahDurrani-7930,

yes,if the first part is true it skips the next part after ||.

1 Vote 1 ·

Got it. Thank you

0 Votes 0 ·