如果Linq的字段为空,如何替换值?

时间:2012-02-25 17:31:16

标签: c# linq

我有以下报告:

var menuItems = _menuRepository.GetPk(pk)
                .Where(m => m.Status == "00")
                .OrderBy(m => m.Order)
                .Select(m => new MenuItem
                {
                    PartitionKey = m.PartitionKey,
                    RowKey = m.RowKey,
                    Order = m.Order,
                    Order1 = m.Order.Substring(0, 1),
                    Order2 = m.Order.Substring(2, 1),
                    Order3 = m.Order.Substring(4, 1),
                    Order4 = m.Order.Substring(6, 2),
                    Title = m.Title,
                    Type = m.Type,
                    Link = m.Link,
                    TextLength = m.TextLength
                });

如果存在Order为null或空的记录,则会失败。有没有办法,如果m.Order为null或为空,我可以替换8个零的字符串,并为所有Order.Substrings替换一个零?

5 个答案:

答案 0 :(得分:7)

您可以使用null-coalescing运算符:

var menuItems = _menuRepository.GetPk(pk)
    .Where(m => m.Status == "00")
    .OrderBy(m => m.Order)
    .Select(m =>
                {
                    var orderOrEmpty = m.Order ?? "00000000";
                    return new MenuItem
                                {
                                    PartitionKey = m.PartitionKey,
                                    RowKey = m.RowKey,
                                    Order = m.Order,
                                    Order1 = orderOrEmpty.Substring(0, 1),
                                    Order2 = orderOrEmpty.Substring(2, 1),
                                    Order3 = orderOrEmpty.Substring(4, 1),
                                    Order4 = orderOrEmpty.Substring(6, 2),
                                    Title = m.Title,
                                    Type = m.Type,
                                    Link = m.Link,
                                    TextLength = m.TextLength
                                };
                });

答案 1 :(得分:2)

Order1 = m.Order == null ? "0" : m.Order.Substring(0, 1)

条件运算符(?:)也称为Ternary运算符,它根据布尔表达式的值返回两个值中的一个。

<boolean condition> ? <value returned if true> : <value returned if false>;

答案 2 :(得分:2)

听起来你想要将m.Order.SubString调用的结果替换为0,如果它是null或为空。执行此操作的方法是,如果m.Order值为null / empty,则只使用全0的字符串

.Select(m => {
   var order = m.Order ?? "00000000";
   return new MenuItem {      
     PartitionKey = m.PartitionKey,
     RowKey = m.RowKey,
     Order = m.Order,
     Order1 = order.Substring(0, 1),
     Order2 = order.Substring(2, 1),
     Order3 = order.Substring(4, 1),
     Order4 = order.Substring(6, 2),
     Title = m.Title,
     Type = m.Type,
     Link = m.Link,
     TextLength = m.TextLength
  }});

答案 3 :(得分:1)

Order1 = (m.Order ?? "00000000").Substring(0, 1)

Order1 = string.IsNullOrEmpty(m.Order) ? "0" : m.Order.Substring(0, 1)

答案 4 :(得分:1)

我会写一个扩展方法IfNotNull并用作

m.Order.IfNotNull(s=>s.Substring(0,1),"0") 
//or
m.Order.IfNotNull(s=>s.Substring(6,2),"00")
//or
m.Order.IfNotNull(s=>s.Substring(6,2))

public static class MyExtensions
{
    public static TOut IfNotNull<T, TOut>(this T target, Func<T, TOut> valueFunc,TOut defValue=default(TOut))
        where T : class
    {
        return target == null ? defValue : valueFunc(target);
    }
}