LINQ check if exists, but without equal to any value

paprykarz

I try to get datas from database. I need to get first product's image url ordered by the lowest position. Action is running on Index view where I have 20 records per page. Code to get image url with first position:

var firstImage = productImageRepository.Get().Where(i => i.ProductId == productId).OrderBy(i => i.Position).First();

In my database only products with id 1-7 have images. When the loop is on product with id 8 and higher, I'm getting and error:

enter image description here

Is there possibility to check if this product's image exists in database? I know I can equal it to some value and check it like if (firstImage != null), but I don't know how to do this without equal position to any value.

Josh Stevens

use FirstOrDefault(), it is just like First() except that if no element match the specified condition than it returns the default value of the underlying type of generic collection.

You can then check if the value is defined which tells you if any items exist in the database, in your case FirstOrDefault() will return null if no images exist in the database.

You can also use Any() with First() as well

My solution would be

var images = productImageRepository.Get().Where(i => i.ProductId == productId).OrderBy(i => i.Position);
if (images.Any()) {
    // could just use First() here now if you wanted 
    // as Any() confirms there is values defined
    return Json(images.FirstOrDefault());
} else {
   // throw a error or something. 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C# Linq check if value exists

SQL check if value exists in a partition using CASE WHEN without any JOIN

React calculator: check if any value exists in string

Check if the value exists in any other columns with Tidyverse

Check if data attribute exists without a value

Check if value exists in range without looping

VBA, Is there a way to check if a value is equal to any value inside array?

How to check if any value in group is equal to a specific value in BigQuery SQL?

Check if any combination of columns are equal some specific value in matrix

C++ how to check if an object is equal to any value in an array?

Check if column exists in linq

Check if any solution exists

Laravel - Is there any blade directive to check if a variable exists and is set to a specific value?

How to check if a value exists in any of the columns in a table in sql

Check if certain value exists is any 4 out of 10 columns

Pandas: Check if value in one df exists in any column of another DF

Pandas: Check if value in one df exists in any column of another DF

Supabase - equal any value

Spark: Check whether a value exists in a nested array without exploding

How do I check if a variable exists before I set a form value equal to it in Jade: Template Engine

Check if any XML nodes exists with Specific Attribute using LINQ C#

How to use a LINQ statement to check if a variable already exists for any object in an existing list before adding that object to the list?

Check if value exists in enum

Check if value exists in file

Check if value exists in vuejs

Check if a value exists in ArrayList

Check if value exists in the list

check if value exists in a group

PHP check if value exists

TOP Ranking

HotTag

Archive