[C#]获取空时串联在 linq 中的字符串

标签: Linq SQLServer .Net C#
发布时间: 2017/2/27 3:47:42
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这是我的查询︰

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 总是返回空值?

解决方法 1:

在 LinqToSql 你要是你 concat 具有 null 值的字符串 null

这是因为查询转化为选择使用+ (字符串连接)

就像对空值执行的算术运算 null 值添加到一个已知值时结果通常都是未知的值,用一个 null 值的字符串串联操作也应产生空的结果。

你可以解决此问题通过使用转换结果与 LinqToObjects AsEnumerable 查询。

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
          });
官方微信
官方QQ群
31647020