I am building a WinForms application that searches for books in the Database.
I have four tables: Author,Book,Country,Genre;
There is a field in "Book" table called "Year". I added startDate and endDate fields, so it can search for specific book released between the given two years(startDate and endDate both are integers). That is where my troubles began.
This is how I parse inputs.
int startDateResult = 0;
int? startDate = null;
if (inputStartDate == string.Empty)
{
startDate = null;
}
else
{
if (Int32.TryParse(inputStartDate, out startDateResult))
{
startDate = startDateResult;
}
else throw new Exception("Please input correct year");
}
int endDateResult = 0;
int? endDate = null;
if (inputEndDate == string.Empty)
{
endDate = null;
}
else
{
if (Int32.TryParse(inputEndDate, out endDateResult))
{
endDate = endDateResult;
}
else throw new Exception("Please input correct year");
}
This is the LINQ query I am using for searching.
specificBookForAuthor = _context.Books.Where(c =>
(c.Author.Name.Contains(First) || c.Author.Surname.Contains(Last))
&& book==string.Empty?true: c.Name.Contains(book)
&& country == string.Empty ? true : c.Author.Country.Name.Contains(country)
&& genre == string.Empty ? true : c.Genre.Name.Contains(genre)
&& inputYear == string.Empty ? true : c.Year==year
&& inputStartDate == string.Empty ? true : c.Year >= startDate
&& inputEndDate == string.Empty ? true : c.Year <= endDate
).Select(b => b).ToList();
This query did not work. Then, I tried to comment all the lines except "inputEndDate", typed 0 in startDate and 5000 in endDate. After Debug, found out, that "specificBookAuthor"-s count was 1. Which was CORRECT.
Commented Code :
specificBookForAuthor = _context.Books.Where(c =>
(c.Author.Name.Contains(First) || c.Author.Surname.Contains(Last))
//&& book==string.Empty?true: c.Name.Contains(book)
//&& country == string.Empty ? true :
// c.Author.Country.Name.Contains(country)
// && genre == string.Empty ? true : c.Genre.Name.Contains(genre)
// && inputYear == string.Empty ? true : c.Year==year
// && inputStartDate == string.Empty ? true : c.Year >= startDate
inputEndDate == string.Empty ? true : c.Year <= endDate
).Select(b => b).ToList();
Did the same with inputStartDate(commented inputEndDate line and uncommented inputStartDate). Worked fine.
I get the problem when I leave both of the fields uncommented. Like this:
specificBookForAuthor = _context.Books.Where(c =>
(c.Author.Name.Contains(First) || c.Author.Surname.Contains(Last))
//&& book==string.Empty?true: c.Name.Contains(book)
//&& country == string.Empty ? true :c.Author.Country.Name.Contains(country)
// && genre == string.Empty ? true : c.Genre.Name.Contains(genre)
// && inputYear == string.Empty ? true : c.Year==year
&& inputStartDate == string.Empty ? true : c.Year >= startDate
&& inputEndDate == string.Empty ? true : c.Year <= endDate
).Select(b => b).ToList();
In that case, "specificBookAuthor"-s count is NULL instead of 1.
Debug.WriteLineorMessageBox.Show). Then debug the query by fixing your inputs and doing what you started doing (commenting out parts) and figuring what is failing as you add in the commented out code a bit at a time.startDateandendDateare null instead of the input strings? In other words, the first part of your question could be removed, it's not relevant.