boruchsiper
boruchsiper

Reputation: 2028

Getting null when concatenating string in linq

this is my query:

var db = new data.MQSDataContextDataContext();
string MonthY = divWave.InnerText.Substring(0, divWave.InnerText.IndexOf(","));
int waveN = Convert.ToInt32(divWave.InnerText.Substring(divWave.InnerText.IndexOf(",") + 1, 2));
int WorsheetID = Convert.ToInt32(Request.QueryString["id"]);
var wsItems = from wi in db.VendorOfferWorsheetItems
              where wi.WorksheetID == Convert.ToInt32(WorsheetID)
              select new resultsForGrid
              {
                  id = wi.id,
                  waveItemID = wi.waveItemID,
                  MName = wi.MName,
                  MQS_Code = wi.MQS_Code,
                  CustomerItemCode = wi.CustomerItemCode,
                  ItemDesc = wi.ItemDesc,
                  UOM = wi.UOM,
                  Ext = wi.Ext,
                  gCost = wi.gCost,
                  Vname = wi.Vname,
                  BestOffer = wi.BestOffer,
                  Margin = wi.Margin,
                  VendorOfferUOM = wi.VendorOfferUOM,
                  ItemWaveViewUrl = "../Waves/ViewWaveItemDetails.aspx?CurrentMonthYear=" + MonthY + "&CurrentWaveN=" + waveN.ToString() + "&Customer=" + divCustomer.InnerText + "&CustomerItemCode=" + wi.CustomerItemCode + "&UOM=" + wi.UOM + "&ItemNo=" + wi.MQS_Code
              };

t-sql (also returns null for ITemWaveViewUrl:

 SELECT ((((@p1 + [t0].[CustomerItemCode]) + @p2) + [t0].[UOM]) + @p3) + [t0].[MQS_Code] AS [ItemWaveViewUrl], [t0].[id], [t0].[waveItemID], [t0].[MName], [t0].[MQS_Code], [t0].[CustomerItemCode], [t0].[ItemDesc], [t0].[UOM], [t0].[Ext], [t0].[gCost], [t0].[Vname], [t0].[BestOffer], [t0].[Margin], [t0].[VendorOfferUOM]
    FROM [dbo].[VendorOfferWorsheetItems] AS [t0]
    WHERE [t0].[WorksheetID] = @p0

why is ItemWaveViewUrl always returning null?

Upvotes: 1

Views: 95

Answers (1)

Johnbot
Johnbot

Reputation: 2179

In LinqToSql if you concat a string with a null value you get null.

This is because the query is translated into a select using the + (String Concatenation):

Just like arithmetic operations that are performed on null values, when a null value is added to a known value the result is typically an unknown value, a string concatenation operation that is performed with a null value should also produce a null result.

You can fix this by transforming the result with LinqToObjects by using AsEnumerable on the query.

var worksheetId = Convert.ToInt32(WorsheetID);
var wsItems = db.VendorOfferWorsheetItems
    .Where(wi => wi.WorksheetID == worksheetId)
    .AsEnumerable()
    .Select(wi => new resultsForGrid
            {
              id = wi.id,
              waveItemID = wi.waveItemID,
              MName = wi.MName,
              MQS_Code = wi.MQS_Code,
              CustomerItemCode = wi.CustomerItemCode,
              ItemDesc = wi.ItemDesc,
              UOM = wi.UOM,
              Ext = wi.Ext,
              gCost = wi.gCost,
              Vname = wi.Vname,
              BestOffer = wi.BestOffer,
              Margin = wi.Margin,
              VendorOfferUOM = wi.VendorOfferUOM,
              ItemWaveViewUrl = "../Waves/ViewWaveItemDetails.aspx?CurrentMonthYear=" + MonthY + "&CurrentWaveN=" + waveN.ToString() + "&Customer=" + divCustomer.InnerText + "&CustomerItemCode=" + wi.CustomerItemCode + "&UOM=" + wi.UOM + "&ItemNo=" + wi.MQS_Code
          });

Upvotes: 3

Related Questions