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

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
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>();

Continue reading...
 
Back
Top