在linq中连接字符串时获取null



这是我的查询:

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(也返回null 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

为什么ItemWaveViewUrl总是返回null?

在LinqToSql中,如果您连接一个字符串与空值,您将获得null

这是因为使用+ (String concatation)将查询转换为选择:

就像对空值执行的算术运算一样,当将空值添加到已知值时,结果通常是一个中执行的字符串连接操作空值也应该产生空结果。

您可以通过在查询中使用AsEnumerable将结果转换为LinqToObjects来解决这个问题。

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
          });

相关内容

  • 没有找到相关文章

最新更新