Where Linq 方法返回不满足条件的元素
Where Linq Method returning elements that don't satisfy condition
我在 List 上调用 Where 方法并且正在 returning 不满足我的条件的元素。
这是我对 Where 方法的调用:
IEnumerable<MyObject> list = returnList.Where(p => p.MaxDate != null && p.MinDate != null);
我希望在 "list" IEnumerable 上只有同时定义了 MaxDate 和 MinDate 的对象(非空)!
和 "list" 以与我的 returnList 相同的结果结束,实际上 "list" 上的项目的 none 作为 MaxDate 和 MinDate 定义的(不同于null),我的 where 条件应该 return 在那种情况下没有元素,对吗?
非常感谢您
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!
}
}
returnList.Where(p => p.MaxDate.HasValue && p.MinDate.HasValue);
工作示例:
https://dotnetfiddle.net/qQrjkC
编辑:即使 != null
也应该有效,您应该在投反对票之前正确地进行测试
天哪,我现在感觉很蠢,我正在查看 IEnumerable 属性 "returnn" 中的字段 "source",而不是检查实际的 ResultsView,我创建了一个 ToList() 和没有返回任何元素!
我很抱歉大声笑,也许有人可以关闭这个问题...
感谢大家的付出!问题出在电脑前(我)哈哈
我在 List 上调用 Where 方法并且正在 returning 不满足我的条件的元素。
这是我对 Where 方法的调用:
IEnumerable<MyObject> list = returnList.Where(p => p.MaxDate != null && p.MinDate != null);
我希望在 "list" IEnumerable 上只有同时定义了 MaxDate 和 MinDate 的对象(非空)!
和 "list" 以与我的 returnList 相同的结果结束,实际上 "list" 上的项目的 none 作为 MaxDate 和 MinDate 定义的(不同于null),我的 where 条件应该 return 在那种情况下没有元素,对吗?
非常感谢您
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!
}
}
returnList.Where(p => p.MaxDate.HasValue && p.MinDate.HasValue);
工作示例: https://dotnetfiddle.net/qQrjkC
编辑:即使 != null
也应该有效,您应该在投反对票之前正确地进行测试
天哪,我现在感觉很蠢,我正在查看 IEnumerable 属性 "returnn" 中的字段 "source",而不是检查实际的 ResultsView,我创建了一个 ToList() 和没有返回任何元素!
我很抱歉大声笑,也许有人可以关闭这个问题...
感谢大家的付出!问题出在电脑前(我)哈哈