Hello,
I am doing this query, somehow I want to check if pl.EmailAddress is null .. How can I fix this?
var filteredPeople = (from pl in people
where pl.FirstName.ToLower().StartsWith(first)
&&
pl.LastName.ToLower().StartsWith(last)
&&
String.IsNullOrEmpty(pl.EmailAddress) ? "" : pl.EmailAddress.ToLower().StartsWith(email))
select pl);
I want the if condition inside the query, thats why I am asking. I won't know pl.EmailAddress outside the query, pl is defined inside the query.. like this.. (from pl in people
var filteredPeople = (from pl in people
where pl.FirstName.ToLower().StartsWith("first")
&&
pl.LastName.ToLower().StartsWith("last")
&&
!string.IsNullOrEmpty(pl.EmailAddress)
select pl);
//Or
var filteredPeople = (from pl in people
where pl.FirstName.ToLower().StartsWith("first")
&&
pl.LastName.ToLower().StartsWith("last")
&&
(pl.EmailAddress == null || pl.EmailAddress.Equals(string.Empty))
select pl);
still not exactly what I wanted.. I want to include if pl.EmailAddress is not null then search on email address too.. EmailAddress.ToLower().StartsWith(email)
var filteredPeople = (from pl in people
where pl.FirstName.ToLower().StartsWith("")
&&
pl.LastName.ToLower().StartsWith("")
&&
(!string.IsNullOrEmpty(pl.EmailAddress) && pl.EmailAddress.StartsWith("abc"))
select pl);
I did somethig like this to make it work.. but I am sure there must be some other elegant way..
List<Person> searchResults;
if (String.IsNullOrEmpty(email))
{
var filteredPeople = (from pl in people
where pl.FirstName.ToLower().StartsWith(first)
&&
pl.LastName.ToLower().StartsWith(last)
select pl);
searchResults = filteredPeople.ToList();
}
else
{
var filteredPeopleOnemail = (from pl in people
where pl.FirstName.ToLower().StartsWith(first)
&&
pl.LastName.ToLower().StartsWith(last)
&&
!string.IsNullOrEmpty(pl.EmailAddress)
select pl);
filteredPeopleOnemail = (from x in filteredPeopleOnemail
where x.EmailAddress.ToLower().StartsWith(email)
select x);
searchResults = filteredPeopleOnemail.ToList();
}
nissan
Participant
1065 Points
618 Posts
Check for null
Apr 24, 2012 08:36 PM|LINK
Hello, I am doing this query, somehow I want to check if pl.EmailAddress is null .. How can I fix this? var filteredPeople = (from pl in people where pl.FirstName.ToLower().StartsWith(first) && pl.LastName.ToLower().StartsWith(last) && String.IsNullOrEmpty(pl.EmailAddress) ? "" : pl.EmailAddress.ToLower().StartsWith(email)) select pl);march11
Contributor
3001 Points
1361 Posts
Re: Check for null
Apr 24, 2012 08:40 PM|LINK
If isDBnull(pl.EmailAddress) then
' Do something
End if
nissan
Participant
1065 Points
618 Posts
Re: Check for null
Apr 24, 2012 08:46 PM|LINK
How can I do that inside the query? that I have..
march11
Contributor
3001 Points
1361 Posts
Re: Check for null
Apr 25, 2012 12:26 PM|LINK
Just like I showed, add your code inside the if condition.
Or try It like this....
If NOT isDBnull(pl.EmailAddress) Then
Else
' Send message to user that email is missing.
End if
nissan
Participant
1065 Points
618 Posts
Re: Check for null
Apr 25, 2012 01:32 PM|LINK
I want the if condition inside the query, thats why I am asking. I won't know pl.EmailAddress outside the query, pl is defined inside the query.. like this.. (from pl in people
march11
Contributor
3001 Points
1361 Posts
Re: Check for null
Apr 25, 2012 01:45 PM|LINK
sorry I don't LINQ that well.
Ramesh T
Contributor
5121 Points
827 Posts
Re: Check for null
Apr 25, 2012 02:01 PM|LINK
Try this
var filteredPeople = (from pl in people where pl.FirstName.ToLower().StartsWith("first") && pl.LastName.ToLower().StartsWith("last") && !string.IsNullOrEmpty(pl.EmailAddress) select pl); //Or var filteredPeople = (from pl in people where pl.FirstName.ToLower().StartsWith("first") && pl.LastName.ToLower().StartsWith("last") && (pl.EmailAddress == null || pl.EmailAddress.Equals(string.Empty)) select pl);nissan
Participant
1065 Points
618 Posts
Re: Check for null
Apr 25, 2012 02:52 PM|LINK
still not exactly what I wanted.. I want to include if pl.EmailAddress is not null then search on email address too.. EmailAddress.ToLower().StartsWith(email)
Ramesh T
Contributor
5121 Points
827 Posts
Re: Check for null
Apr 25, 2012 03:00 PM|LINK
Is this wat u want?
var filteredPeople = (from pl in people where pl.FirstName.ToLower().StartsWith("") && pl.LastName.ToLower().StartsWith("") && (!string.IsNullOrEmpty(pl.EmailAddress) && pl.EmailAddress.StartsWith("abc")) select pl);nissan
Participant
1065 Points
618 Posts
Re: Check for null
Apr 25, 2012 03:00 PM|LINK
I did somethig like this to make it work.. but I am sure there must be some other elegant way..
List<Person> searchResults; if (String.IsNullOrEmpty(email)) { var filteredPeople = (from pl in people where pl.FirstName.ToLower().StartsWith(first) && pl.LastName.ToLower().StartsWith(last) select pl); searchResults = filteredPeople.ToList(); } else { var filteredPeopleOnemail = (from pl in people where pl.FirstName.ToLower().StartsWith(first) && pl.LastName.ToLower().StartsWith(last) && !string.IsNullOrEmpty(pl.EmailAddress) select pl); filteredPeopleOnemail = (from x in filteredPeopleOnemail where x.EmailAddress.ToLower().StartsWith(email) select x); searchResults = filteredPeopleOnemail.ToList(); }