none
LINQ: How to handle situation when joining two object & when one is null RRS feed

  • Question

  • see my below code. here i am joining two list<t> object QCViewAllBrokerList and customformulaList  but some time one could be null. my below query throwing error when 

    customformulaList is null. i want to write a query which will work smoothly when one object is null. please guide me how to restructure my below code when one linq object is null. thanks

    var QCViewAllHistValue = (from viewalllst in QCViewAllBrokerList
    			  join frmlst in customformulaList
    			  on new
    			  {
    			      val = String.IsNullOrEmpty(viewalllst.ViewAllSection) ? "" : viewalllst.ViewAllSection.Trim().ToUpper(),
    			      val1 = String.IsNullOrEmpty(viewalllst.ViewAllLineItem) ? "" : viewalllst.ViewAllLineItem.Trim().ToUpper(),
    			      val2 = String.IsNullOrEmpty(viewalllst.ViewAllPeriod) ? "" : viewalllst.ViewAllPeriod.Replace("A", "").Replace("E", "").Trim().ToUpper(),
    			      val3 = String.IsNullOrEmpty(viewalllst.ViewAllBroker) ? "" : viewalllst.ViewAllBroker.Trim().ToUpper()
    			  }
    
    			  equals new
    			  {
    			      val = String.IsNullOrEmpty(frmlst.Section) ? "" : frmlst.Section.Trim().ToUpper(),
    			      val1 = String.IsNullOrEmpty(frmlst.Li) ? "" : frmlst.Li.Trim().ToUpper(),
    			      val2 = String.IsNullOrEmpty(frmlst.Period) ? "" : frmlst.Period.Replace("A", "").Replace("E", "").Trim().ToUpper(),
    			      val3 = String.IsNullOrEmpty(frmlst.Broker) ? "" : frmlst.Broker.Trim().ToUpper()
    
    			  }
    			  into tempJoin
    			  from leftJoin in tempJoin.DefaultIfEmpty()
    			  where viewalllst.Wtg == "1"
    			  select new QCHelper()
    			  {
    			      Broker = viewalllst.ViewAllBroker == null ? string.Empty : viewalllst.ViewAllBroker,
    			      Section = viewalllst.ViewAllSection == null ? string.Empty : viewalllst.ViewAllSection,
    			      Li = viewalllst.ViewAllLineItem == null ? string.Empty : viewalllst.ViewAllLineItem,
    			      Period = viewalllst.ViewAllPeriod == null ? string.Empty : viewalllst.ViewAllPeriod,
    			      CrossCalc1Q = leftJoin == null ? string.Empty : leftJoin.CrossCalc1Q,
    			      CrossCalc2Q = leftJoin == null ? string.Empty : leftJoin.CrossCalc2Q,
    			      CrossCalc3Q = leftJoin == null ? string.Empty : leftJoin.CrossCalc3Q,
    			      CrossCalc4Q = leftJoin == null ? string.Empty : leftJoin.CrossCalc4Q,
    			      CrossCalcFY = leftJoin == null ? string.Empty : leftJoin.CrossCalcFY,
    			      Value = viewalllst.Value == null ? string.Empty : viewalllst.Value,
    			      QCFormula = leftJoin == null ? string.Empty : leftJoin.QCFormula,
    			      CustomFormula = leftJoin == null ? string.Empty : leftJoin.CustomFormula,
    			      Historical = leftJoin == null ? string.Empty : String.IsNullOrEmpty(leftJoin.Historical) ? "" : leftJoin.Historical,
    			      DeriveCrossCalc = leftJoin == null ? string.Empty : leftJoin.DeriveCrossCalc
    			  }).ToList<QCHelper>();


    • Edited by Sudip_inn Saturday, February 9, 2019 4:37 PM
    Saturday, February 9, 2019 4:36 PM

Answers

  • To avoid the error, simply ensure that the list is not null:

    customformulaList = customFormulaList ?? new List<yourClass>();

    In this way, if the list is null it will be replaced by an empty list, and this will let the linq query be processed without error.

    Of course, if the list is empty, the join will not produce any rows, so you might as well not bother. Simply write an "if" that skips the whole processing if the list is null.

    • Marked as answer by Sudip_inn Sunday, February 10, 2019 5:45 PM
    Sunday, February 10, 2019 11:46 AM
    Moderator