简体   繁体   English

当我尝试对DateTime进行排序时,为什么我的Linq查询会抛出异常?

[英]Why does my Linq query throw an exception when I try and sort on DateTime?

I have this method that is meant to filter my blog entries by month... 我有这个方法,用于按月过滤我的博客条目...

public IList<KeyValuePair<string, int>> GetBlogsByMonth(PteDotNetCore.EnumsAndConstants.BlogType blogTypeArg)
        {
            var blogsOfType = from b in dataContext.blogs
                               where b.BlogType == (int)blogTypeArg
                               select b;


            IList<KeyValuePair<string, int>> retVal = new List<KeyValuePair<string, int>>();

            for(int i= 12; i > 1; i--)
            {
                var blogsInMonth = from b in blogsOfType
                                   where ((DateTime)b.DateAdded).Month == DateTime.Now.AddMonths(-i).Month
                                   select b;

                KeyValuePair<string, int> keyToAdd = new KeyValuePair<string, int>(DateTime.Now.AddMonths(-i).Month.ToString(), blogsInMonth.ToList().Count);

                retVal.Add(keyToAdd);

            }

            return retVal;

        }

When I use the debugger blogsInMonth I get an error 当我使用调试器blogsInMonth时,我收到一个错误

base {System.SystemException} = {"Nullable object must have a value."}

Blog is an entity framework object. 博客是一个实体框架对象。 To be honest Im a bit confused as to why I have to look in the Value of the blog.DateTime object. 说实话我有点困惑为什么我要查看blog.DateTime对象的值。 Even when I change it to... 即使我把它改成......

var blogsInMonth = from b in blogsOfType
                                   where ((DateTime)b.DateAdded).Month == DateTime.Now.AddMonths(-i).Month
                                   select b;

It still doesnt work...Hope someone can help... 它仍然无法工作...希望有人可以帮助......

Most likely the Blog.DateAdded column is nullable in the database and there exists null values in the column. 很可能Blog.DateAdded列在数据库中可以为空,并且列中存在空值。

You can try 你可以试试

where b.DateAdded != null && ((DateTime)b.DateAdded).Month == DateTime.Now.AddMonths(-i).Month

I think this is because your b.DateAdded is null. 我认为这是因为你的b.DateAdded为null。 Try changing the lines at the top as follows: 尝试更改顶部的行,如下所示:

var blogsOfType = from b in dataContext.blogs
    where b.DateAdded != null && b.BlogType == (int)blogTypeArg
    select b;

As a matter of optimization, you can calculate "i moths back" outside of your Linq expression. 作为优化问题,您可以在Linq表达式之外计算“i moths back”。 In case there are many blog entries, you wouldn't be doing DateTime.Now.AddMonth(-1) more times than necessary. 如果有许多博客条目,您将不会比必要时多做DateTime.Now.AddMonth(-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试使用WIA进行扫描时,为什么我的代码会引发异常? - Why does my code throw an exception when I try to scan using WIA? 当我尝试获取类型的计数时,为什么LINQ查询会抛出异常 - Why does LINQ query throw an exception when I attempt to get a count of a type 为什么这个查询会抛出异常? - why does this query throw an exception? 为什么表达式中的LINQ查询会引发此错误? - Why does a LINQ query in an expression throw this error? LINQ to SQL为什么在尝试将新对象添加到数据库时认为我的新对象为空? - Why does LINQ to SQL think think that my new object is null when I try to add it to the database? 为什么在应该表现为try / catch / finally的情况下使用using会引发异常? - Why does using throw an exception when it's supposed to behave like try/catch/finally? 如果我尝试在父类中使用TransactionScope,为什么EF&NUnit会引发异常? - Why does EF & NUnit throw an Exception if I try and use TransactionScope in a parent class? 为什么我的自定义事件会引发异常? - Why does my custom event throw an exception? 为什么我的ServiceStack服务会抛出异常? - Why does my ServiceStack service throw an exception? 为什么在 linq 表达式的 where 子句中调用局部变量时会引发此异常? - Why does this exception throw when calling a local variable in a where clause of a linq expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM