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.
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";
}
您可以这样尝试:
var j = db.Rentals.Where(s=>s.MovieTitle.Contains(tb_movietitle.Text).Select(s=>s.takenbacktime ).FirstOrDefault();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.