【发布时间】:2013-04-10 09:53:31
【问题描述】:
使用 EntityFramework,我可以使用以下语法获取某种类型的实体列表:
List<Customer> customers = ((IQueryable<Customer>)myEntities.Customers
.Where(c => c.Surname == strSurname)
.OrderBy(c => c.Surname)).ToList<Customer>();
然后我可以这样做以只得到我感兴趣的数据:
var customerSummaries = from s in customers
select new
{
s.Surname, s.FirstName, s.Address.Postcode
};
我得到了包含所请求汇总数据的字段(以及必要时的表格)的strings 列表(基于用户选择)。例如,对于上面的“customerSummary”,提供的strings 列表将是:“Surname”、“FirstName”、“Address.Postcode”。
我的问题是:如何将该字符串列表转换为仅提取指定字段所需的语法?
如果无法做到这一点,那么对于列列表,什么类型(比字符串)更好,以便我可以提取正确的信息?
如果有意义的话,我想我需要知道 EF [entity|table] 的 [member|column|field] 的类型。
编辑:
我尝试了建议的答案 - 动态 linq - 使用以下语法
string parmList = "Surname, Firstname, Address.Postcode";
var customers = myEntities.Customers.Select(parmList)
.OrderBy("Address.Postcode");
但这会导致:EntitySqlException was unhandled. 'Surname' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly.
所以,一个后续问题。我是否正确使用Select?我只看到使用Where 和OrderBy 子句的示例,但我认为基于这些我做对了。
如果我的 Select 语法不是问题,谁能看到是什么问题?
编辑 2:它是颠倒的。这有效:
string parmList = "Surname, Firstname, Address.Postcode";
var customers = myEntities.Customers
.OrderBy("Address.Postcode")
.Select(parmList);
【问题讨论】:
-
您可以使用其他方法吗?例如存储过程?它可能不是 linq,但如果它是一次性的,那么实现起来会非常简单。
-
我曾考虑过使用直接 sql,如果/当我被击败时,我会尝试一下,但还不是现在。 :-)
标签: c# linq entity-framework