【发布时间】:2009-06-19 20:43:21
【问题描述】:
我有一段我认为非常简单的代码,但结果让我感到困惑。我正在使用 LINQ 查询实体,然后遍历结果以创建一个数组。我正在查看进出数据库的流量,那里的一切看起来都很好。当我将 LINQ 发送到 SQL 的查询复制并直接针对 SQL 运行时,我得到了预期的结果。然而,当我对结果进行迭代时——或者甚至对结果进行观察——每条记录都是完全相同的。这 NOT 是 SQL 返回的内容。我做错了什么?
var eventList = from e in entityContext.AuctionSet select e;
ArrayList DayArray = new ArrayList();
int i = 0;
foreach (Auction ae in eventList)
{
// If I put a watch on eventList all the records are the same!
Auction temp = ae; // Even tried copying to a temp value per another solution
// Even though the records are different in the database,
// and the correct number of records are returned, EVERY "ae" instance
// has the exact same values!
DayArray.Add(new {
id = i,
title = temp.County.ToString()
});
i++;
}
谢谢!
编辑:忘记提及实体来自视图,考虑到有关主键的评论,这将是有意义的。
【问题讨论】:
-
只是出于好奇,您为什么要使用 ArrayList?你为什么不使用 List
? -
“eventList”的类型是什么?
-
使用 ArrayList 因为在这段代码之后它被序列化为 JSON。
-
(回复评论并建议修复)
标签: c# linq entity-framework