I'm retrieving tuples from a MSSQL Database with this query and List:
string query3 = @"select * from HistoryMessageValues where IdMessage in (select id from HistoryMessages where IDDevice in ("+listIDs+")) and (DtRicezione between '" + fromdate.ToString("yyyy-dd-MM") + "' and '" + todate.ToString("yyyy-dd-MM") + "')";
List<HistoryMessageValues> totalm= dbMngr.Set<HistoryMessageValues>().FromSql(query3).ToList();
List<HistoryMessageValues> outdoor = totalm.Where(x => x.IdDataModelField==Guid.Parse("9f5d1fe3-27d7-44ee-bea4-02b36897f9af")).ToList();
The query wrote explicitly is this:
select * from HistoryMessageValues where IdMessage in (select id from HistoryMessages where IDDevice in ('7b6d6ca2-4d87-4334-9477-d96925d992c4' )) and (DtRicezione between '2010-01-01' and '2019-01-08')
and IdDataModelField='9f5d1fe3-27d7-44ee-bea4-02b36897f9af'
If I execute the query itself directly to the DB, I'll obtain these tuples:

As you can see, attributes "value" and "DtRicezione" are different (at least, most part of them) over tuples.
Meanwhile, I'm obtaining only the first tuple replicated hundreds.
Indeed, executing the following cycle, after that 3 lines of C# in my backend:
List<HistoryMessageValues> outdoor = totalm.Where(x => x.IdDataModelField==Guid.Parse("9f5d1fe3-27d7-44ee-bea4-02b36897f9af")).ToList();
for (int i=0; i<outdoor.Count(); i++)
{
Console.WriteLine(outdoor.ElementAt(i).value + " " + outdoor.ElementAt(i).DtRicezione + " " + outdoor.ElementAt(i).IdDataModelField);
}
The result is this:

As you can compare the two images, that will be the first tuple available into the DB's table.
Table's script creation:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[HistoryMessageValues](
[id] [bigint] NOT NULL,
[IdDataModelField] [uniqueidentifier] NOT NULL,
[value] [nvarchar](50) NOT NULL,
[IdMessage] [bigint] NOT NULL,
[DtRicezione] [datetime] NULL,
[idKey] [uniqueidentifier] NULL
) ON [PRIMARY]
GO
Istorn!
Try to use foreach instead your for...
outdoor.ForEach( x => Console.WriteLine(x.value + " " + x.DtRicezione + " " + x.IdDataModelField));
Try to check you datareader inside your query executor. maybe it not going to the next row (so your list will receive every time the same row from database).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With