【发布时间】:2020-02-05 02:43:51
【问题描述】:
我正在使用 Automapper 和带有 Xml 序列化的类来生成 XML 文件。
在这个方法中,它返回一个 IEnumerable 给 Automapper,然后 Automapper 写出一系列<GenerationMethod>...</GenerationMethod> XML 元素。
它确实有效,但是,如果它返回一个空的 IEnumerable,因为没有找到结果,我会得到像这样的空 XML 标记:
<GenerationMethod />
有没有办法返回 NULL 以免生成空的 XML 标记?
方法如下。谢谢!
public static IEnumerable<GenerationMethod> GetGenerationMethod(this DungeonGrid monster)
{
var customMonster = monster.Stats
.Where(e => e.Stat.Category.IsActive);
if (monster.MonsterType.DestructionMethod.StartsWith("TEST"))
{
yield return new GenerationMethod(monster.MonsterType.DestructionMethod);
}
foreach (Stat in customMonster)
{
if (DungeonLookupByStatId.ContainsKey(customMonster.MonsterType.Id))
yield return DungeonLookupByStatId[customMonster.MonsterType.Id];
}
}
【问题讨论】:
标签: c# asp.net-core automapper ienumerable