2

Ive been having some trouble trying to get this linq statement to work. I am setting up a search using linq queries. What i want to do is if a search is null or empty, have it ignore that part of the filtering. So what i have set up is many where clauses that short circuit the where clause like so:

tvContent.LoadContent(
    Live.Ringtones
        .Where(x => cbSeller.SelectedValue== null || 
               x.Property.SellerID == (int)cbSeller.SelectedValue)
        .Where(x => cbProperty.SelectedValue==null || 
               x.PropertyID == (int)cbProperty.SelectedValue)
        .Where(x => string.IsNullOrEmpty(tbContentID.Text) || 
               x.RingtoneID == ContentID)
        .Where(x => string.IsNullOrEmpty(tbContentName.Text) || 
               x.RingtoneName == tbContentName.Text).ToList());

But when I do this I keep getting null reference issues. cbProperty, is empty, and selectedValue does show up null when I debug, but it still says there's a null reference issue. What am I doing wrong?

juharr
  • 31,741
  • 4
  • 58
  • 93
Mr. MonoChrome
  • 1,383
  • 3
  • 17
  • 39
  • see: http://stackoverflow.com/questions/1079556/why-isnt-this-short-circuit-in-lambda-working – Habib Nov 17 '14 at 20:21
  • Due to the nature of the question (EF vs. Linq-To-SQL) and the fact that the answers are different, I'm OK with this being re-opened. A person searching on the internet would be drawn to the good title on this question; and if they saw the other question, may not realize it could pertain to their issue. In general, try not to take actions that seem to create a conflict of interest. In this specific case, if the question had been flagged for re-opening, I'd re-open it. – George Stocker Nov 17 '14 at 21:51
  • @GeorgeStocker Nothing about this question or its answer has anything to do with the query provider. The behavior is specific to the LINQ methods in the System.LINQ namespace. It's not *possible* for a query provider to alter this behavior if it *tried* to. The query provider is entirely superfluous information. – Servy Nov 18 '14 at 15:23
  • @Servy Given that a new user may not understand that; and that this question is better answered than the other; I'm leaving it open. You can always take this to meta and raise an issue about it. – George Stocker Nov 18 '14 at 15:25
  • @GeorgeStocker Better answered? This question doesn't answer a single thing! It explains nothing about what is going on, what the behavior is, or how to go about solving the problem. It's just a code dump that is only there to even possibly help one person with their exact code. The other question explains the behavior, how to solve it, and is generalized to apply to every situation for every LINQ provider for everyone, ever. The answer to this question has zero lasting value. The author of this question can copy-paste it without actually understanding what's going on, and that's it. – Servy Nov 18 '14 at 15:28
  • @Servy Please take it to meta.SO. The comments are not the place to make an extended argument. We have meta just so we have a place to make general policies and to resolve issues about specific questions. – George Stocker Nov 18 '14 at 15:30

1 Answers1

2

Why are you putting an invariant into the where clauses?

var ringtones = Live.Ringtones;

if (cbSeller.SelectedValue!= null)
    ringtones = ringtones.Where(x=> x.Property.SellerID 
                                     == (int)cbSeller.SelectedValue);

if (cbProperty.SelectedValue!= null)
    ringtones = ringtones.Where(x=> x.PropertyID 
                                     == (int)cbProperty.SelectedValue);

if(!string.IsNullOrEmpty(tbContentID.Text))
    ringtones.Where(x=> x.RingtoneID == ContentID)

if(!string.IsNullOrEmpty(tbContentName.Text) )
    ringtones.Where(x => x.RingtoneName == tbContentName.Text)

tvContent.LoadContent(ringtones.ToList());
James Curran
  • 101,701
  • 37
  • 181
  • 258