该entities
变量仅包含对表的引用。您应该在上下文范围内具体化您的数据,这样您就可以像
IQueryable<M> entities = null;
List<M> realEntities = null;
using (var context = new DataContext("MySql", ConnectionString))
{
entities = context.GetTable<M>();
// materialize entities in scope of the context
realEntities = entities.ToList();
}
return realEntities;
您还可以在物化之前执行一些过滤:
using (var context = new DataContext("MySql", ConnectionString))
{
entities = context.GetTable<M>();
// you can apply Where filter here, it won't trigger the materialization.
entities = entities.Where(e => e.Quantity > 50);
// what exactly happens there:
// 1. Data from the M table is filtered
// 2. The filtered data only is retrieved from the database
// and stored in the realEntities variable (materialized).
realEntities = entities.ToList();
}
我建议您研究一个关于物化的主题。