实体SQL-奇怪



我正在使用从对象上下文中获取数据。CreateQuery(sqlStatement,parameterValues)。请注意,这是我用作字符串常量的实体SQL,它被传递到CreateQuery方法中,而不是我源代码中编译的LINQ查询。

这非常有效。但是有一些重复的记录。。。

ctx.CreateQuery<DbDataRecord>(@"SELECT  record.home_city, record.home_stabb 
FROM Employees AS record  
ORDER BY record.home_city SKIP 0 LIMIT 15",prms);

所以我认为这将删除重复:

ctx.CreateQuery<DbDataRecord>(@"SELECT DISTINCT record.home_city, record.home_stabb 
FROM Employees AS record  
ORDER BY record.home_city SKIP 0 LIMIT 15",prms);

完全相同的语句,完全相同的对象上下文。然而,当我添加DISTINCT修饰符时,我会得到下面的异常。我知道所有的列都是正确的,因为第一条语句非常有效。由于出现异常,它似乎与ORDERBY子句有问题。

有什么东西我遗漏了吗?这对我来说毫无意义。

System.Data.EntitySqlException was unhandled by user code
Message='record.home_city' 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. Near member access expression, line 1, column 95.
Source=System.Data.Entity
Column=95
ErrorContext=member access expression, line 1, column 95

你能在下面尝试一下,然后运行一个distinct吗?我知道区别是你们面临这个问题的主要原因,但至少你们知道另一种解决方法

ctx.CreateQuery<DbDataRecord>(@"SELECT VALUE record FROM Employees AS record  
ORDER BY record.home_city SKIP 0 LIMIT 15",prms);

最新更新