“Cannot call methods on DateTime”, and other limitations

后端 未结 5 1551
离开以前
离开以前 2020-12-30 11:32

Does anyone know of a definitive list of LINQ to SQL query limitations that are not trapped at compile time, along with (where possible) workarounds for the limitations?

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 12:27

    I had exactly this issue with DateTimes, and found that currently the following workaround works for me, but I realise that with larger result sets, it could get to be an issue as the processing is now in my app rather than on the database:

    BlogPosts post = (from blogs in blogPosts
               where blogs.PostPath == path 
               select blogs)
               .ToList()
               .Where(blogs => blogs.Published.Date == publishedDate.Date)
               .SingleOrDefault();
    

    Note the ".ToList()" in the middle - this casts it to a normal IEnumerable and allows me to use the usual properties I'd expect.

    One thing that still confuses me slightly however is the fact that this is legal in EF:

    BlogPosts posts = from blogs in blogPosts
               where !blogs.IsDraft
                 && blogs.Published.Year == year
                 && blogs.Published.Month == month
               orderby blogs.Published descending
               select blogs
    

    So, I can call ".Year" or ".Month" on a DateTime, but not ".Date" - I guess it comes down to types.

提交回复
热议问题