none
LINQ query on checkbox list then UNION their results - error of NULL RRS feed

  • Question

  • I have spend 5 days trying on LINQ checkboxes that chk1, chk2

          IQueryable<Profile> temp;

          var temp1 = (chk1 == false) ? null : db.Profiles.Where(i => (i.Point1 > 0) || (i.Point1 < 0));

          var temp2 = (chk2 == false) ? null : db.Profiles.Where(i => (i.Point2 > 0) || (i.Point2 < 0));

          temp = (temp1.Union(temp2).Distinct().OrderByDescending(t => t.Lead_ID);

    However, some result returned with NULL caused error of System.ArgumentNullException: 'Value cannot be null.

    Do you have any suggestion ?

    Wednesday, January 23, 2019 4:27 AM

All replies

  • Check this:

    var temp1 = chk1 ? db.Profiles.Where(i => i.Point1 != 0) : Enumerable.Empty<Profile>( );

    var temp2 = chk2 ? db.Profiles.Where(i => i.Point2 != 0) : Enumerable.Empty<Profile>( );

     

    or this:

    temp = db.Profiles.Where( i => (chk1 && i.Point1 != 0) || (chk2 && i.Point2 != 0)).OrderByDescending(t => t.Lead_ID);

    Wednesday, January 23, 2019 6:23 AM