LINQ to SQL的 - 如何选择特定的列,并返回强类型列表 [英] LINQ to SQL - How to select specific columns and return strongly typed list
问题描述
我试图使用LINQ到SQL从表中选择一些特定的列,并返回结果为对象的强类型列表。
I'm trying to use LINQ to SQL to select a few specific columns from a table and return the result as a strongly typed list of objects.
例如:
var result = (from a in DataContext.Persons
where a.Age > 18
select new Person
{
Name = a.Name,
Age = a.Age
}
).ToList();
任何帮助将是很大的AP preciated。
Any help would be greatly appreciated.
它建立正常,但是当我运行它,我得到的错误。实体类型的显式建筑 myEntity所
查询是不允许的。
It builds fine, but when I run it, I get the error. Explicit construction of entity type MyEntity
in query is not allowed.
推荐答案
的问题是在事实的属性之一是另一个表的关系。我改变了我的LINQ查询,以便它可以得到从不同的方法相同的数据,而无需加载整个表。
The issue was in fact that one of the properties was a relation to another table. I changed my LINQ query so that it could get the same data from a different method without needing to load the entire table.
感谢大家的帮助!
这篇关于LINQ to SQL的 - 如何选择特定的列,并返回强类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!