gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:40 25 4
gpt4 key购买 nike

这是我的查询:

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

 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 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.

您可以通过在查询中使用 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
});

关于c# - 在 linq 中连接字符串时获取 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30357059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com