El código linq-to-sql no devuelve resultados a pesar de que el sql generado SÍ devuelve resultados

I have the following code in my application

var query = context.xosAssets.Where(x => x.GSA == 0).Take(INDEX_ASSET_QUERY_COUNT);
var assets = query.ToList();

// Debug
string message = string.Format("Assets waiting for indexing: {1}{0}Database:{3}{0}Query: {2}", 
    Environment.NewLine, query.Count(), query.ToString(), context.Connection.Database);
    System.Diagnostics.EventLog.WriteEntry("GSAFeed", message, System.Diagnostics.EventLogEntryType.Information);

Cuando esto se ejecuta, query.Count() es cero y assets.Count is zero because no results are returned. However, this is incorrect as ALL records in this database have their GSA field set to zero.

When I take the output from query.ToString() and run it (replacing @p0 with 0), I get all the correct responses. I have verified that I"m on the exact same database as is being outputted by context.Connection.Database and I am running out of options on what can be wrong.

Why is Linq-to-sql returning no results even though the actual sql does?

preguntado el 09 de marzo de 12 a las 15:03

Que es INDEX_ASSET_QUERY_COUNT ¿ajustado a? -

GSA is a short, and INDEX_ASSET_QUERY_COUNT is a int const set at 250, which is correctly shown in the TOP() call in the outputted SQL. editar eve nwithout the Top call it still fails. -

Have you tried running SQL profiler and seeing what is actually being run on the database? -

I actually just thought of that, but in the trace I"m not seeing any db requests for the tables i'm trying to hit, which doesn't make much sense -

Are you building a new context/connection or reusing an existing one? If the latter, are you starting with an empty table and populating it either with a different context or using a non-linq method? If so Linq2Sql might be using cached results. -

1 Respuestas

Ive ran into problems like this in the past, I generally re-write the Linq2Sql in long form & its gotten me out of trouble. Not expert enough to know why but it may help you.

EDITAR Algo como esto :

var query = (from x in context.xosAssets
            where x.GSA == 0
            select x).Take(INDEX_ASSET_QUERY_COUNT);

This should return IQueryable so no need to perform .ToLIst()

respondido 09 mar '12, 17:03

What do you mean in long form? - kalldrexx

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.