Linq-to-SQL: Ignore null parameters from WHERE clause?

Num) { Console. WriteLine(num. Value); } and you get the same error It should work if you do this: var q = ( from c in db.

Contacts where c. Active == true && c. LastReviewedOn p.OwnerUserId.

Value == ownerUserId. Value); return q.Count().

Your issue is that your are not passing in a nullable int, you are passing in a null. Try this: Print(null); private void Print(int? Num) { Console.

WriteLine(num. Value); } and you get the same error. It should work if you do this: var q = ( from c in db.

Contacts where c. Active == true && c. LastReviewedOn p.OwnerUserId.

Value == ownerUserId. Value); return q.Count().

If yes, c. OwnerUserId could be null and not having any value in c.OwnerUserId.Value.

1. Also: so, if you query for userId=3 and some contact has no userId, you'll be doing (!ownerUserId. HasValue || c.OwnerUserId.

Value == ownerUserId. Value) = (!{3}. HasValue || c.

{null}. Value == {3}. Value), and accessing {null}.

Value throws an Exception. – ANeves Mar 26 '10 at 14:06 Good point which I didn't consider - however I changed the clause to ( c.OwnerUserId. HasValue && ownerUserId.

HasValue && c.OwnerUserId. Value == ownerUserId. Value ) to test and was still seeing the same error message – Peter Bridger Mar 26 '10 at 14:10.

OwnerUserId, List ownerGroupIds) { var x = ( from c in db. Contacts where c. Active == true && c.

LastReviewedOn AddDays(-365) && ( // Owned by group ownerGroupIds. Count == 0 || ownerGroupIds. Contains( c.OwnerGroupId.

Value ) ) select c ); if (ownerUserId. HasValue) { x = from a in x where c.OwnerUserId. Value == ownerUserId.

Value } return x.Count(); }.

PROBLEM: "&&" and "||" is converted to a method like "AndCondition(a, b)", so "!a. HasValue || a. Value == b" becomes "OrCondition(!a.

HasValue, a. Value == b);" The reason for this is probably to get a generic solution to work for both code and SQL statements. So instead, use the "?

:" notation. For more, see my blog post: peetbrits.wordpress.com/2008/10/18/linq-... // New revised code. Public static int NumberUnderReview(int?

OwnerUserId, List ownerGroupIds) { return ( from c in db. Contacts where c. Active == true && c.

LastReviewedOn Value == ownerUserId. Value ownerUserId. HasValue?C.OwnerUserId.

Value == ownerUserId. Value : true ) && ( // Owned by group // ownerGroupIds. Count == 0 || // ownerGroupIds.

Contains( c.OwnerGroupId. Value ) ownerGroupIds. Count!

= 0? OwnerGroupIds. Contains( c.OwnerGroupId.

Value ) : true ) select c ).Count(); }.

Null && ownerUserId. Where(p => p.OwnerUserId. Value == ownerUserId.

Have you some contacts with OwnerUserId null? If yes, c. OwnerUserId could be null and not having any value in c.OwnerUserId.

What about conditionally adding the where clause to the expression tree? Public static int NumberUnderReview(int? Var x = ( from c in db.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions