Iterate over LINQ entity results

I have what I thought was a very simple piece of code, but the results puzzled me. I request objects using LINQ and then iterate over the results to create an array. I look at traffic to and from the database, and everything looks good there. When I copy a query that LINQ sends to SQL and runs it directly with SQL, I get the expected results. However, when I repeat the results - or even set the clock on the results - each record is exactly the same. This is NOT what SQL returns. What am I doing wrong?

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++;
 }

Thanks!

EDIT: Forgot to mention that entities come from a view, which makes sense given the comment on the primary key.

+3
1

, , EF , - , ? , EF , + , .

, :

id   | title | ...
-----+-------+-------
1    | Foo   | ...
1    | Bar   | ...
1    | Baz   | ...
...

EF , id , , Foo, Foo, Foo...

+7

Source: https://habr.com/ru/post/1710873/


All Articles