gpt4 book ai didi

c# - 如果返回的行为 NULL,则将 List 项设置为空

转载 作者:行者123 更新时间:2023-12-02 22:14:38 25 4
gpt4 key购买 nike

我需要填充 List<string>但是当我的查询返回空值时,不会在此列表中创建空字符串。

var maskObj = (from m in context.sistema_DocType_Index
join n in context.sistema_Indexes on m.indexId equals n.id
where m.id == docTypeId
select new maskModel
{
mask = n.mask
}).ToList();


return Content(""+maskObj.Count());

如果这个查询返回 3 行,并且它们都是 NULL ,我需要列表中的 3 个空字符串。有什么办法可以实现这一目标吗?

据我所知,对于 Sql Server,NULL 是没有值(value)的。即使 NULL == NULL 返回 false,那么我该如何继续?

最佳答案

我想你想要像下面这样的东西:

mask = n.mask ?? string.Empty

这使用了 null-coalescing operator如果 n.mask 为 null,则使用 string.Empty

Entity Framework 生成的SQL应该是这样的:

COALESCE(MASK, '')

当您说“如果此查询返回 3 行,并且所有行均为 NULL”时,我的回答假设您是在谈论 masknull

关于c# - 如果返回的行为 NULL,则将 List<T> 项设置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14690777/

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