如果列表为空,则添加默认字符串[英] Adding a default string if list empty

本文是小编为大家收集整理的关于如果列表为空,则添加默认字符串的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在写出一个汇总的状态列表.除了没有的情况外,它可以正常工作.目前,渲染无效,位置为空.

item.Stuff.Where(e => Condition(e))
  .Select(f => f.Status)
  .Aggregate(String.Empty, (a, b) => a + b)

现在,我想用" --- "填充表元素,以防列表通过条件将列表过滤为一个空的元素 't决定一种方法.

什么是一种平稳的方法?

我尝试了下面的暴行之类的东西,但看起来好吧……很残酷,它也不是正确的.我可以看到实际的源代码行(先于 false 或 true ),而不是值.

item.Stuff.Where(e => Condition(e)).Count() < 1
  ? "---"
  : item.Stuff.Where(e => Condition(e))
    .Select(f => f.Status)
    .Aggregate(String.Empty, (a, b) => a + b)

推荐答案

您可以做这样的事情.如果状态列表相当小(否则应该使用 StringBuilder ,而不是字符串串联).

item.Stuff.Where(e => Condition(e))
    .Select(f => f.Status)
    .Aggregate("---", (a, b) => (a == "---") ? b : (a + b));

它检查是否更换了默认文本,如果是,则将下一个状态元素连接到已经存在的文本质量.

,如果并且仅当它从未进行评估,即列表为空,则将返回" --- ".否则将获得与以前相同的结果.


如果Status是一个枚举,您只需要不同的状态,就可以使用[flags]属性的行为.

如果您定义了这样的枚举

[Flags]
enum Status
{
    None = 0,
    Active = 1,
    Inactive = 2,
    Pending = 4,
    Deleted = 8
    ...
}

您可以做:

item.Stuff.Where(e => Condition(e))
  .Aggregate(Status.None, (a, b) => a | b)

结果是列表中存在的所有状态的集合,并且如果从未运行,输出格式列表(Active, Inactive, Pending>)或None.

其他推荐答案

您可以使用DefaultIfEmpty(string.Empty).First(); msdn

示例

var item =  item.Stuff.Where(e => Condition(e))
  .Select(f => f.Status)
  .Aggregate(String.Empty, (a, b) => a + b);

var newitem = item.DefaultIfEmpty(new Item() { i = 1, Status = "---" }).First();

本文地址:https://www.itbaoku.cn/post/1557026.html

问题描述

I'm writing out an aggregated list of statuses. It works fine except for the situation where there are none. At the moment, null is rendered and the position is empty.

item.Stuff.Where(e => Condition(e))
  .Select(f => f.Status)
  .Aggregate(String.Empty, (a, b) => a + b)

Now, I'd like to populate the table element with "---" in case the list is filtered down to an empty one by Condition but I can't decide on a method.

What would be a smooth way to approach it?

I've tried something like the atrocity below but it looks, well..., atrociously and it doesn't render right, neither. I get to see the actual source code line (preceded by False or True) instead of the values.

item.Stuff.Where(e => Condition(e)).Count() < 1
  ? "---"
  : item.Stuff.Where(e => Condition(e))
    .Select(f => f.Status)
    .Aggregate(String.Empty, (a, b) => a + b)

推荐答案

You could do something like this. If the status list is reasonably small (otherwise one should use StringBuilder anyway and not string concatenation).

item.Stuff.Where(e => Condition(e))
    .Select(f => f.Status)
    .Aggregate("---", (a, b) => (a == "---") ? b : (a + b));

It checks if the default text was replaced and, if it was, it concatenates the next status element to the already existing text mass.

This will return "---" if and only if it's never evaluated, i.e. if the list is empty. Otherwise one would get the same result as previously.


If Status is an enum, and you only need the distinct statuses, you can use the behavior of the [Flags] attribute.

If you define the enum like this

[Flags]
enum Status
{
    None = 0,
    Active = 1,
    Inactive = 2,
    Pending = 4,
    Deleted = 8
    ...
}

you can just do:

item.Stuff.Where(e => Condition(e))
  .Aggregate(Status.None, (a, b) => a | b)

The result is a collection of all statuses that are present in the list, and the output is nicely formatted list (Active, Inactive, Pending) or None if it's never run.

其他推荐答案

You can use DefaultIfEmpty(string.Empty).First(); MSDN

Example

var item =  item.Stuff.Where(e => Condition(e))
  .Select(f => f.Status)
  .Aggregate(String.Empty, (a, b) => a + b);

var newitem = item.DefaultIfEmpty(new Item() { i = 1, Status = "---" }).First();