1

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?

4
  • 1
    Are you sure that ItemWaveViewUrl is null? Is it possible that instead wsItems is null becuase there was no match on WorksheetID ? Commented May 20, 2015 at 17:59
  • I'm getting 3000 results with values except for ItemViewUrl Commented May 20, 2015 at 18:01
  • get rid of everything from the + onwards, check if the value is not null, add the next + variable, check if the value is not null, etc. Or use String.Format("../url?cv={0}&sec={1} etc", wi.gCost, wi.Margin) Commented May 20, 2015 at 18:07
  • 1
    string.Format will not translate to SQL I think. The string concatenation occurs in SQL, not LINQ. Commented May 20, 2015 at 18:09

1 Answer 1

3

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
          });
Sign up to request clarification or add additional context in comments.

4 Comments

From some quick googling, it seems the interwebs agree with you. Still, a reference to some official documentation would make this an even better answer.
You are right, CustomerItemCode is null in all records.
LINQ2SQL only translates your coded intent to the SQL of the underlying database. This is why the second approach works, because wi is already an instance of a client type, thanks to AsEnumerable(). The only challenge here, if VendorOfferWorsheetItems has additional 100 columns, you'd be bringing them in as well, thus retrieving more data than you actually need.
@Darek Absolutely correct. This can be remedied by having an additional select before the AsEnumerable with only the relevant columns selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.