Asked by:
How to do Or in linq to sql

Question
-
User-240513752 posted
try { DateTime todaysDateTime = DateTime.Now.AddDays(-1); IQueryable<FalckBookingSearch> bookingSearches = null; using (var db = new SpiderEntities()) { bookingSearches = from uc in db.V_ENT_BOOKINGS where uc.USER_ID == userId && uc.LANGUAGE_CODE == language && uc.FROMDATE > todaysDateTime select new FalckBookingSearch { Firstname = uc.FIRSTNAME, Surname = uc.SURNAME, Booking_ID = uc.BOOKING_ID, FromDate = uc.FROMDATE, PersonCompanyName = uc.ORDER_COMPANY, CourseName = uc.COURSENAME, CourseCode = uc.COURSECODE_CODE, ToDate = uc.TODATE, OrderID = uc.WS_ORDER_ID, BookingStatus = uc.BOOKING_STATUS, PersonID = uc.PERSON_ID }; if (personId != null && personId.Value != 0) { bookingSearches = bookingSearches.Where(s => s.PersonID == personId); } if (!String.IsNullOrEmpty(model.FilterFirstName)) { bookingSearches = bookingSearches.Where(s => s.Firstname.ToLower().Contains(model.FilterFirstName.ToLower())); } if (!string.IsNullOrEmpty(model.SearchCourseName)) { bookingSearches = bookingSearches.Where(s => s.CourseName.ToLower().Contains(model.SearchCourseName.ToLower())); } if (!string.IsNullOrEmpty(model.CourseCode)) { bookingSearches = bookingSearches.Where(s => s.CourseCode.ToLower().Contains(model.CourseCode.ToLower())); } if (model.SearchBookingID != 0) { bookingSearches = bookingSearches.Where(s => s.Booking_ID == model.SearchBookingID); } if (!string.IsNullOrEmpty(model.FilterCompanyName)) { bookingSearches = bookingSearches.Where(s => s.PersonCompanyName.ToLower().Contains(model.FilterCompanyName.ToLower())); } if (model.SearchOrderID != 0) { bookingSearches = bookingSearches.Where(s => s.OrderID == model.SearchOrderID); } if (model.FromDate.HasValue) { bookingSearches = bookingSearches.Where(s => s.FromDate > model.FromDate); } if (model.ToDate.HasValue) { bookingSearches = bookingSearches.Where(s => s.ToDate < model.ToDate); } if (!string.IsNullOrEmpty(model.FilterBookingStatus)) { bookingSearches = bookingSearches.Where(x => x.BookingStatus == model.FilterBookingStatus); } return bookingSearches.ToList().OrderByDescending(x => x.Booking_ID).ToList(); } }
How to change it to OR ,right now it is working as AND
Friday, December 15, 2017 6:37 AM
All replies
-
User1120430333 posted
How to change it to OR ,right now it is working as AND
This is C# 101 basics you should know.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator
Monday, December 18, 2017 12:44 AM -
User-1838255255 posted
Hi Khan_1,
According to your description, as far as i know, && represent and, || represent or, so if you want to use or, please try use ||.
For more details, please check the following tutorial:
Conditional OR Operator:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/#conditional-or-operator
Note: Use && and || are likely that will generate two different datasets, so if you want to change your code logic?
Best Regards,
Eric Du
Tuesday, December 19, 2017 6:42 AM -
User-240513752 posted
I know the it could be done by OR clause, but how would I implement Or in the above code.
This is my question.
Wednesday, December 20, 2017 3:56 AM -
User-1838255255 posted
Hi khan_1,
As far as i know, AND and OR only are operators, the meaning of them, i have explain it in the before reply, you say want to implement it in your code, I think you could replace && with || through your needs. If my understanding is right, please correct me!
Best Regards,
Eric Du
Thursday, December 21, 2017 2:45 AM