IQueryable.Where clause AND not working with multiple search parameters - asp.net-mvc

I am having an issue with getting a Multiple search working with a IQueryable.Where Clause using the AND operator. I know that the problem is when a parameter is blank or null it is searching for a "" with the other parameter and not returning results but I don't want to have to go through an extended and ridiculously nested if to check for nulls or blanks on 8 parameters. I have googled this for days, the OR works obviously but the AND condition returns nothing.
albums = albums.Where(a => a.CA_AlbumName.ToUpper().Contains(searchName.ToUpper()) &&
a.CA_AlbumURL.ToUpper().Contains(searchURL.ToUpper()));
The above works if both parameters are not blank but returns nothing if one of them is. I have tried building the where based on not null like below but same results:
if (!String.IsNullOrEmpty(searchName))
{
albums = albums.Where(a => a.CA_AlbumName.ToUpper().Contains(searchName.ToUpper()));
}
if (!String.IsNullOrEmpty(searchURL))
{
albums = albums.Where(a => a.CA_AlbumURL.ToUpper().Contains(searchURL.ToUpper()));
}

If I understand your requirement right, the following one should work:
albums = albums.Where(a =>
(
string.IsNullOrEmpty(searchName) ||
a.CA_AlbumName.ToUpper().Contains(searchName.ToUpper())
) &&
(
string.IsNullOrEmpty(searchURL) ||
a.CA_AlbumURL.ToUpper().Contains(searchURL.ToUpper())
)
);
#James is right. The updated code is here:
var isSearchNameNullOrEmpty = string.IsNullOrEmpty(searchName);
var isSearchURLNullOrEmpty = string.IsNullOrEmpty(searchURL);
albums = albums.Where(a =>
(
isSearchNameNullOrEmpty ||
a.CA_AlbumName.ToUpper().Contains(searchName.ToUpper())
) &&
(
isSearchURLNullOrEmpty ||
a.CA_AlbumURL.ToUpper().Contains(searchURL.ToUpper())
)
);

Related

Entity Framework query exception when looking for an entity contained in a collection

I'm running the query below to obtain all the events (as a registered student) I'm attending on a specific day and getting an error that says
System.NotSupportedException: Unable to create a constant value of type 'YogaBandy.Models.Profile.YogaProfile'. Only primitive types or enumeration types are supported in this context.
Here is the query I'm using to get all events I'm regsitered for on a specific day.
// my profile
var yogaProfile = dbContext.YogaProfiles.Where(i => i.ApplicationUserId == userId).First();
// events I'm registered for on a specific day
var eventsNew = dbContext.YogaSpaceEvents.Where(
i => i.EventDateTime.Day == date.Day
&& i.EventStatus == YogaSpaceEventStatus.Active
&& i.RegisteredStudentsNew.Contains(yogaProfile)).ToList();
I think it might have something to do with part, but not sure
&& i.RegisteredStudentsNew.Contains(yogaProfile)
FYI - my RegisteredStudentsNew looks like this in the 'YogaSpaceEvents' entity
public virtual ICollection<YogaProfile> RegisteredStudentsNew { get; set; }
and when I add a newly regsitered student I add him/her like this
spaceEvent.RegisteredStudentsNew.Add(yogaProfile);
dbContext.SaveChanges();
Try to move your YogaProfiles.Where(i => i.ApplicationUserId == userId) inside Include statement.
Example:
var eventsNew = dbContext.YogaSpaceEvents
.Include(p=>p.RegisteredStudentsNew.Where(rp => rp.ApplicationUserId == userId))
.Where( i => i.EventDateTime.Day == date.Day
&& i.EventStatus == YogaSpaceEventStatus.Active)
.ToList();
OR
use Any in your where clause
var eventsNew = dbContext.YogaSpaceEvents
.Include(p=>p.RegisteredStudentsNew)
.Where(i => i.EventDateTime.Day == date.Day
&& i.EventStatus == YogaSpaceEventStatus.Active
&& i.RegisteredStudentsNew.Any(rp => rp.ApplicationUserId == userId))
.ToList();
Please read this for why use Include in LINQ.

LINQ - joining two tables and getting the values from the right part of the join if it exists throw exception [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
I have a table of questions and a table of answers
I always need to display the list of questions regardless and there is a corresponsing asnwer, I need to grab that answer (response)
I use the following code
var Questions = db.Questions.Where(x => x.isActive);
var Answers = db.Answers.Where(x => x.AssessmentID == 99);
AssessmentResponseVM model = new AssessmentResponseVM();
foreach (var question in Questions)
{
AnswerAndQuestions q = new AnswerAndQuestions { };
q.QuestionText = question.QuestionText;
q.QuestionID = question.ID;
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault().Response; <--- throws exception if there is no answer for the question
model.Questions.Add(q);
}
But get this error
Object reference not set to an instance of an object.
On this line
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault().Response;
change
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault().Response;
to this code
q.Response=Answers.Any(a=>a.QuestionID==question.ID)?Answers.firstOrDefault(a => a.QuestionID == question.ID).Response:new Response();
q.Response = Answers.Any(a => a.QuestionID == question.ID) ? Answers.Where(a => a.QuestionID == question.ID).FirstOrDefault().Response;
If you're allowed to use C# 6.0, I would suggest to try new null-conditional operator.
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault()?.Response;
It returns null when Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault() returns default value, which is null.
If you need something else, not null, you can use null-coalescing operator.
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault()?.Response ?? response, what you need;

linq: how to get sorted records from last ID

I have the following table,
ItemTable,
Col1 : ItemID(int)
Col2 : MRP(Decimal)
To one of the application I needed to pass selected number of items at a time, They will send me the lastId which I passed to them, the initial requirement was to pass the newest items, which I was able to get it using following query,
var itemList = itemRepository.AsQueryable()
.Where(r => r.ProductID == productID && r.IsActive == true && r.ItemID< lastId)
.OrderByDescending(r => r.ItemID)
.Take(numberOfItems)
.ToList();
However now there is a sort option added to it, which is the MRP column, though again i have only the last Id with me, how could I will able to get this? I tried with the following query, no luck.
var itemList = itemRepository.AsQueryable()
.Where(r => r.ProductID == productID && r.IsActive == true && r.ItemID< lastId)
.OrderByDescending(r => r.ItemID)
.OrderBy(r => r.MRP)
.Take(numberOfItems)
.ToList();
UPDATE : Working Code
As per CamperWill's suggesstion I updated my code and works great with skip.
var itemList = itemRepository.AsQueryable()
.Where(r => r.ProductID == productID && r.IsActive == true)
.OrderBy(r => r.MRP)
.Skip(pageNumber * numberOfItems)
.Take(numberOfItems)
.ToList();
LastID will not help you with paging if you are sorting by a different field (MRP). Also, as indicated in the comments above, the first ordering is effectively ignored by adding the second.
You could consider tracking the page number that is requested and use the Skip() extension.
// input parameter 'page'
var itemList = itemRepository.AsQueryable()
.Where(r => r.ProductID == productID && r.IsActive == true)
.OrderBy(r => r.MRP)
.Skip( (page-1)*numberOfItems )
.Take(numberOfItems)
.ToList();

syntax error in simple LINQ statement

I am developing MVC app and I am using the LINQ in controller.
I am trying to get one rechord with below query, but its giving an error...
Approval oAP = new Approval();
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
Is there any wrong syntax ?
Got the answer
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount)).FirstOrDefault();
Change this
e.ApprovedBy.Id = loggedEmployee.Id
For
e.ApprovedBy.Id == loggedEmployee.Id
You're comparing not assigning values.
Also you may add this
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount)).FirstOrDefault();
Because i'm assuming that you want to return only one
Some remarks:
You should be able to drop the Where:
oAP = db.Approvals.FirstOrDefault(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
Personally, I try to avoid the First and FirstOrDefault functions, because if you know there is only one record and if you want to enforce this, you can use SingleOrDefault:
oAP = db.Approvals.SingleOrDefault(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
If you know there will always be (more than) one record, you can drop the 'OrDefault' part and use First() or Single().

Speeding up a linq query

I am trying to replicate the following SQL query with linq. On SQL Server it takes a fraction of a second to run:
select g.reference, count(*)
from isis.dbo.[group] as g inner join
isis.dbo.enrolment as e on g.groupid = e.groupid inner join
isis.dbo.student as s on e.studentid = s.studentid inner join
isis.dbo.progression as p on s.studentid = p.studentid
where p.academicyear = '12/13' and g.istutorgroup = 1
group by reference
In my MVC application I am passing a listing of "TutorGroups" to the view. For each tutor group in the view I need to display various information about them, one item being the number of "Progression" interviews they have had.
I have tried a couple of methods but they both take upwards of 30 secs to run in my MVC application:
<%TTMrequired = tg.Enrolments
.SelectMany(e => e.Student.Progressions
.Where(p => p.TTMChecked == false &&
p.TTMInterview == true &&
p.AcademicYear == year))
.Count(); %>
and
<%TTMrequired = tg.Enrolments
.Where(e => e.Student.Progressions
.Any(p => p.TTMChecked == false &&
p.TTMInterview == true &&
p.AcademicYear == year))
.Count(); %>
Anyone got any suggestions on how I can speed this up? I suspect the problem is me trying to do it a stupid way - it usually is!
You could try doing a Sum of counts instead of a SelectMany:
tg.Enrolments.Sum(e => e.Student.Progressions
.Count(p => p.TTMChecked == false &&
p.TTMInterview == true &&
p.AcademicYear == year)
);
Should be written using this syntax:
var TTMRequired = (from g in tg.Groups
join e in tg.Enrolment on g.groupid equals e.groupid
join s in tg.Students on e.studentid equals s.studentid
join p in tg.Progressions on s.studentid = p.studentid
where p.academicyear.Equals("12/13") && g.istutorgroup.Equals(1)
group g by g.reference into grp
select new {
grpRef = grp.Key,
grpCount = grp.Count()
});
Note: if g.istutorgroup is of type BIT instead of INT, consider using .Equals(true).

Resources