【问题标题】:Implicit convert List<int?> to List<int>将 List<int?> 隐式转换为 List<int>
【发布时间】:2013-01-01 20:58:09
【问题描述】:

我正在使用 Linq to Entities。

有一个实体“Order”,它有一个可为空的列“SplOrderID”。

我将我的订单列表查询为

List<int> lst = Orders.where(u=> u.SplOrderID != null).Select(u => u.SplOrderID);

我理解这是因为 SplOrderID 是一个可为空的列,因此 select 方法返回可为空的 int。

我只是希望 LINQ 有点聪明。

我该如何处理?

【问题讨论】:

    标签: c# linq list linq-to-entities


    【解决方案1】:

    在选择属性时,只需获取可为空的值:

    List<int> lst =
      Orders.Where(u => u.SplOrderID != null)
      .Select(u => u.SplOrderID.Value)
      .ToList();
    

    【讨论】:

      【解决方案2】:

      linq

      var lst = (from t in Orders
                 where t.SplOrderID.HasValue
                 select new Order
                 {
                   SplOrderID = t.SplOrderID
                 }).Select(c => c.SplOrderID.Value).ToList();
      

         var lst = (from t in Orders
                     where t.SplOrderID.HasValue
                     select t.SplOrderID.Value).ToList();
      

      【讨论】:

      • 那是List&lt;Order&gt;,而不是List&lt;int&gt;
      • 现在是List&lt;int?&gt;,而不是List&lt;int&gt;
      【解决方案3】:

      我发现你的问题试图解决同样的问题,经过几次尝试,我得到了这个解决方案,为select创建的列表中的每个属性投射int

      List<int> lst = Orders.where(u=> u.SplOrderID != null).Select(u => (int)u.SplOrderID);
      

      【讨论】:

        【解决方案4】:

        有用的帮助/扩展方法:

        我通常对其他答案中提到的工作使用一些辅助扩展方法:

        public static class IEnumerableExtensions
        {
            public static IEnumerable<TKey> GetNonNull<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey?> keySelector) 
                where TKey : struct
            {
                return source.Select(keySelector)
                    .Where(x => x.HasValue)
                    .Select(x => x.Value);
            }
        
            // the two following are not needed for your example, but are handy shortcuts to be able to write : 
            // myListOfThings.GetNonNull()
            // whether myListOfThings is List<SomeClass> or List<int?> etc...
            public static IEnumerable<T> GetNonNull<T>(this IEnumerable<T?> source) where T : struct
            {
                return GetNonNull(source, x => x);
            }
        
            public static IEnumerable<T> GetNonNull<T>(this IEnumerable<T> source) where T : class
            {
                return GetNonNull(source, x => x);
            }
        
        }
        

        在你的情况下使用:

        // will get all non-null SplOrderId in your Orders list, 
        // and you can use similar syntax for any property of any object !
        
        List<int> lst = Orders.GetNonNull(u => u.SplOrderID);
        

        对于不想在转换时简单地忽略空值的读者

        值得一提的是GetValueOrDefault(defaultValue) 的潜在用途,也许您希望保留原始空值,但将它们转换为一些默认/标记值。 (作为defaultValue 参数给出):

        以你为例:

        // this will convert all null values to 0 (the default(int) value)
        List<int> lst =
             Orders.Select(u => u.GetValueOrDefault())
             .ToList();
        
        // but you can use your own custom default value
        const int DefaultValue = -1;
        List<int> lst =
             Orders.Select(u => u.GetValueOrDefault(DefaultValue))
             .ToList();
        

        【讨论】:

        • 对于downvoter:任何解释总是很感激,所以我可以改进我的答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 2016-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多