gpt4 book ai didi

c# - EF 中的常量导致异常

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:23 24 4
gpt4 key购买 nike

我有以下代码。但它会导致异常。

                using (var context = new blogEntities())
{
var listOfComments = context.Comments
.OrderByDescending(c => c.CreateDate)
.Where(c => c.CreateDate > fromDate)
.Select(c => new NewsFeedData()
{
ArticleID = c.ArticleID,
CommentID = c.CommentID,
Text = c.CommentText,
Author = c.Author,
CreateDate = c.CreateDate,
Type = 'C'
}).ToList();
}

比我试过枚举但有一些问题。实现我想要的最好方法是什么?我想为 Type 分配一些常量

最佳答案

一种简单的方法是从数据库中获取所有值到一个匿名类型,然后在最终投影之前使用 AsEnumerable 切换到 LINQ to Objects:

using (var context = new blogEntities())
{
var listOfComments = context.Comments
.OrderByDescending(c => c.CreateDate)
.Where(c => c.CreateDate > fromDate)
.Select(c => new { c.ArticleID, c.CommentID, c.CommentText,
c.Author, c.CreateDate })
.AsEnumerable() // Switch into LINQ to Objects
.Select(c => new NewsFeedData
{
ArticleID = c.ArticleID,
CommentID = c.CommentID,
Text = c.CommentText,
Author = c.Author,
CreateDate = c.CreateDate,
Type = 'C'
}).ToList();
}

关于c# - EF 中的常量导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11265682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com