【发布时间】:2016-05-17 15:00:57
【问题描述】:
我在 List 上调用 Where 方法并返回不满足我的条件的元素。
这是我对 Where 方法的调用:
IEnumerable<MyObject> list = returnList.Where(p => p.MaxDate != null && p.MinDate != null);
我希望在“列表”IEnumerable 中仅包含定义了 MaxDate 和 MinDate 的对象(非空)!
并且“列表”以与我的 returnList 相同的结果结束,实际上“列表”上没有定义为 MaxDate 和 MinDate 的项目(不同于 null),在这种情况下,我的 where 条件应该不返回任何元素,对吗?
非常感谢您
EDIT2(我添加了我正在使用的命名空间,可能存在一些错误):
简单示例:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
class MyObject
{
public DateTime? MinDate { get; set; }
public DateTime? MaxDate { get; set; }
public string Description{ get; set; }
}
static void Main(string[] args)
{
List<MyObject> lista = new List<MyObject>();
lista.Add(new MyObject { Description = "123" });
lista.Add(new MyObject { Description = "456" });
lista.Add(new MyObject { Description = "678" });
IEnumerable<MyObject> returnn = lista.Where(p => p.MinDate != null && p.MaxDate != null); //this list contains 3 elements and should have 0!! Microsoft bug???? I can't understand this!
}
}
【问题讨论】:
-
如果没有重现问题的方法,就很难判断出了什么问题。请在您的问题中包含minimal reproducible example - 应该没问题。
-
抱歉,我现在编辑了我的问题;)
-
您的代码为零项。
-
什么鬼?严重地??我的机器怎么了??
-
您如何确认
returnn不为空?如果将它放在 行之后,会发生什么?Console.WriteLine("Items in returnn: " + returnn.ToList().Count);
标签: linq conditional-statements where