c# - 在 LINQ 内部连接中保留空值
问题描述
此代码执行内部连接special
,things
然后生成things
相应匹配项的索引:
object[] special = { 'b', null, 5.0f, "notme" };
object[] things = { "a", 'b', 3, null, 5.0f, 6.0 };
var specialThingIndices = from tn in things.Select((t,n) => (t,n))
join s in special on tn.t equals s
select tn.n;
foreach (var n in specialThingIndices)
Console.WriteLine($"[{n}] = {Convert.ToString(things[n])}, {things[n] is null}");
它输出:
[1] = b, False
[4] = 5, False
但是,null
出现在两个数组中,我不想跳过它。所以我希望它输出的是:
[1] = b, False
[3] = , True
[4] = 5, False
为什么它看不到null
s,我怎样才能让它保留它们?
我尝试在各个地方手动申请DefaultIfEmpty()
,但没有成功。我也尝试过替换equals
,==
但我想那不是有效的语法。我想不出别的。
可运行的示例在这里。
解决方案
如果为 null,您可以选择默认值,例如"<null>"
字符串。
...
object[] special = { 'b', null, 5.0f, "notme" };
object[] things = { "a", 'b', 3, null, 5.0f, 6.0 };
var specialThingIndices = from tn in things.Select(x => x ?? "<null>").Select((t, n) => (t, n))
join s in special.Select(x => x ?? "<null>") on tn.t equals s
select tn.n;
foreach (var n in specialThingIndices)
Debug.WriteLine($"[{n}] = {Convert.ToString(things[n])}, {things[n] is null}");
...
Where
使用而不是编辑
另一个解决方案join
。
...
object[] special = { 'b', null, 5.0f, "notme" };
object[] things = { "a", 'b', 3, null, 5.0f, 6.0 };
var specialThingIndices = from tn in things.Select((t, n) => (t, n))
where special.Any(s => Equals(s, tn.t))
select tn.n;
foreach (var n in specialThingIndices)
Debug.WriteLine($"[{n}] = {Convert.ToString(things[n])}, {things[n] is null}");
...
推荐阅读
- reactjs - 阿波罗-除非有2个换行符,否则什么都不会发生...?
- arrays - 如何使用分隔符连接参数?
- server - 如何将自定义 JNDI 资源添加到类似 Wildfly-10 中
glassfish 服务器? - python - django.db.utils.IntegrityError:NOT NULL 约束失败:polls_article.reporter_id
- android - 如何获取布局对象
- node.js - 在节点 js API 中管理更多数量的服务器请求
- php - phpmailer 发送一封电子邮件后停止发送电子邮件
- c++ - G++ 无法编译 Win7、64 位 (MinGW.org GCC-6.3.0-1)
- python - ““缩进中制表符和空格的使用不一致”
- javascript - Angular 4 声明文件错误