Reputation: 93
I have a database where if the selected dvd's is not taken back (so the date of the arrival is empty) it should write to the linklabel that that the dvd is still rented:
var j = (from s in db.Rentals where s.MovieTitle == tb_movietitle.Text
select s.takenbackdate).FirstOrDefault();
if (j == null)
{
linkLabel1.Text = "The dvd is still rented";
}
else
{
linkLabel1.Text = "Rentable";
}
If I use First()
it says that it is empty, but if I use FirstOrDefault()
it shows null
to all movies even if they have taken back date in the database.
Upvotes: 0
Views: 4264
Reputation: 12954
When the documentation talk about empty, they talk about the source, the list of elements.
So if FirstOrDefault
and First
get a source that has no elements, the default value will be returned or an exception is thrown.
'Empty' does not refer to 'empty' values, like a null-value.
To get what you want, try this:
// Find the first DVD with the given title. If not found, an exception is thrown.
var j = (from s in db.Rentals where s.MovieTitle == tb_movietitle.Text).First();
// If the taken back date is null, it is still rented.
if (j.takenbackdate == null)
{
linkLabel1.Text = "The dvd is still rented";
}
else
{
linkLabel1.Text = "Rentable";
}
Upvotes: 1
Reputation:
You can try like this:
var j = db.Rentals.Where(s=>s.MovieTitle.Contains(tb_movietitle.Text).Select(s=>s.takenbacktime ).FirstOrDefault();
Upvotes: 0